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

How do I get a variable to refer to something?

Asked by
Zerio920 285 Moderation Voter
9 years ago

For example, I have something similar to this in a localscript:

local value = game.Players.NumberValue.Value

But when I try manipulating it, like

function add()
    value = 0
    value = value + player.Character.Torso:GetMass()
end

The numbervalue doesn't change. The value variable changes, but the numbervalue doesn't. Any help would be appreciated.

1 answer

Log in to vote
3
Answered by
dyler3 1510 Moderation Voter
9 years ago

The problem is with your function. You have no connection line, and you're not calling it. Also, you can't access the NumberValue's value directly like that.

local value = game.Players.NumberValue
print(value)

Having the value set as this stores the value as a number. Not the object itself. This would print whatever the NumberValue's Value is, not the NumberValue. For example, if the NumberValue's Value was set at 5, this would print out "5" instead of "NumberValue".


For your script to work, we need it to be set as the object, which is "NumberValue". To fix this, try the script below:

local value = game.Players.NumberValue --Sets the object, instead of it's Value

function add()
    value.Value = 0 --Accesses the NumberValue's Value.
    value.Value = value.Value + player.Character.Torso:GetMass()
end

add() --This is all you need to run the function, and the NumberValue's Value will change.

Anyways, this should work now. If you have any problems or questions, please leave a comment below. Hope I helped :P

2
dyler, I think line 08 should be add() not function(). woodengop 1134 — 9y
0
Yeah. Also, if you wanted to connect it to a touched event for example, you'd do something like part.Touched:connect(add) at the bottom of the script instead of add(), where part is the path to the part that's going to be touched. Spongocardo 1991 — 9y
0
Great answer! Thank you so much, the help is very appreciated. Zerio920 285 — 9y
0
I was aware of how to use a function beforehand, btw. The function I presented was just an example. Just assume it's called later in the script. I get the gist of it now, though. I should be setting the variable to the object and not the value. Thanks again. Zerio920 285 — 9y
0
No prob. Glad I could help, and thanks for pointing that out ContinentofEurope, I fixed it. dyler3 1510 — 9y
Ad

Answer this question