Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do you subtract from an IntValue?

Asked by 7 years ago

I made this script that tests if Cash (My IntValue) has a value of 10 or more and if so it subtracts 10, but the IntValue remains the same after joining the game and clicking the part. I know the error is the subtraction because I have some other unimportant parts in this script that run fine. I just can't figure out how to subtract from an IntValue?

Heres the script I made:

script.Parent.ClickDetector.MouseClick:connect(function()
    local Cash = workspace.Cash.Value
if Cash >= 10 then
    Cash = -10
end)

if anybody could help me, I can't seem to figure out how to subtract from an IntValue.

1 answer

Log in to vote
1
Answered by
mraznboy1 194
7 years ago

Sure, the issue you have is a pretty common one in that you're looking at the value of the IntValue rather than subtracting from the actual reference itself. When you instantiate the variable "Cash", you create a new variable that is separate from the IntValue Cash, as workspace.Cash is an object in the workspace, while Cash = workspace.Cash.Value means it is simply an int variable, which would be no different than doing Cash = 10. Therefore, when you subtract 10 from Cash, everything is actually working perfectly, as Cash is indeed 10-10=0 now, but workspace.Cash was never modified. The fix for this is to simply reference workspace.Cash as local Cash, then compare Cash.Value and modify Cash.Value instead like so:

script.Parent.ClickDetector.MouseClick:connect(function()
    local Cash = workspace.Cash
if Cash.Value >= 10 then
    Cash.Value = Cash.Value - 10
end)
0
Thank a lot! barrettr500 53 — 7y
Ad

Answer this question