Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

lua - IntValue always is equal to 0 even though it isn't

So I'm having a problem where I'm trying to get the value of an IntValue but it always gives me 0, even if the value is not 0. There is no error that appears and it is inside a server script. I can't tell what the problem is:

local leaderstats = Instance.new("Folder", plr)
leaderstats.Name = "leaderstats"

local Clicks = Instance.new("IntValue", leaderstats)
Clicks.Name = "Clicks"

local Multiplier = Instance.new("IntValue", leaderstats)
Multiplier.Name = "Multiplier"

-- a bunch of datastore stuff


game.Players.PlayerRemoving:Connect(function(plr)
print("removing")
local approved, errortext = pcall(function()
    print(plr.leaderstats.Clicks.Value, plr.leaderstats.Multiplier.Value)
    datastore:SetAsync(plr.UserId.."-save", {plr.leaderstats.Clicks.Value, plr.leaderstats.Multiplier.Value})
    
end)

if approved then
    
    print("data saved")
    print( plr.leaderstats.Multiplier.Value, plr.leaderstats.Clicks.Value)
else
    
    print("idiot data wont save")
    
end

end)

And it just prints 0 for the multiplier value every single time.

This is the code that changes the multiplier value:

local leaderstats = game.Players.LocalPlayer:WaitForChild("leaderstats")
local clicks = leaderstats.Clicks
local folder = script.Parent.multipliers
local multiplier = leaderstats.Multiplier

for i,v in pairs(folder:GetChildren()) do

local cost = v.Cost

v.MouseButton1Click:Connect(function()
    
    print(v)
    
    if multiplier.Value > tonumber(v.Name) then
        
        print("already bbogt")
        
    end
    
    if multiplier.Value + 1 == tonumber(v.Name) then
        
        if cost.Value > clicks.Value then
            
            print("not enough moneys smh my head")
            
        elseif cost.Value <= clicks.Value then
            
            print("rich smh mynhead")
            
            clicks.Value -= cost.Value
            
            multiplier.Value = multiplier.Value + 1
        end
        
        
    elseif multiplier.Value == 0 and v.Name == "2" then
        if 5 > clicks.Value then

            print("not enough moneys smh my head")

        elseif 5 <= clicks.Value then

            print("rich smh mynhead")

            clicks.Value -= 5

            multiplier.Value = 2
            print(clicks.Value)
        end
        
    end
    
end)

end
question from:https://stackoverflow.com/questions/65894149/intvalue-always-is-equal-to-0-even-though-it-isnt

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

How are you changing the Value of the Multiplier? When You change the value of the Multiplier from a script, check manually from the explorer if the value is not 0. For example in this code, it does change the variable's value but not the actual Intvalue's value.

local Player = game.Players.LocalPlayer
local Intvalue = Player.leaderstats.IntValue.value

IntValue = IntValue + 10

Basically what happens here is that the game gets the value of IntValue and returns that as the variable. Then when you change the value of the variable, it does increase the value of the variable, but it does not change the actual value of the intvalue in the Explorer. So it would be better to do it this way:

local Player = game.Players.LocalPlayer
local IntValue = Players.leaderstats.IntValue

IntValue.value = IntValue.value + 10

Basically this changes the value of the actual Intvalue in the explorer instead of the other example where it just changes the variable. So If you would print the Variable "IntValue" in the first example, it would still print 10 like the second example, but that would print the variable's value and not the actual one.

What I thought was that you might be changing the value of it the wrong way so that it doesn't actually change it, and so it says "0" in the output. But if the value is in-fact not 0 in the explorer, then I don't really know if I can help much.

Also I am not that good of an explainer, So if I did not explain my point well, Then basically just check the Value of the Multiplier manually in the explorer. And make sure that you are changing the actual value of the Multiplier, and not just the variable inside of an script somewhere.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...