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

I cant add to a value?

Asked by 9 years ago

If I want to add 1 to a NumValue when a part is touched, but its not working

Can someone help please?

This is what I have so far

function onTouch()
    game.Workspace:FindFirstChild("NumValue").Value +1
end

Help?

2 answers

Log in to vote
3
Answered by
drew1017 330 Moderation Voter
9 years ago

It's not working because there's nothing telling the script that +1 is the value that needs to be added.

Additionally, ''Value = +1'' won't work either, because +1 is not a valid numerical assignment in logical mathematics. However, we can use those mathematics after we've already assigned a valid numerical; Therefore, we need to set the value to itself plus 1 to get the desired effect.

function onTouch()
    game.Workspace:FindFirstChild("NumValue").Value = game.Workspace:FindFirstChild("NumValue").Value + 1
end

1
And you have to connect the function. funyun 958 — 9y
Ad
Log in to vote
0
Answered by
funyun 958 Moderation Voter
9 years ago
function onTouch()
    NumValue = workspace:FindFirstChild("NumValue")
    NumValue.Value = NumValue.Value + 1 --Set it to its own value plus 1. 1 + 1 = 2; 2 + 1 = 3; 3 + 1 = 4; etc
end

script.Parent.Touched:connect(onTouch) --Remember to connect the function!

Answer this question