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

NumberValue is changed and printed through script but stays is the same in the workspace editor?

Asked by 7 years ago
01local health = script.Parent.Parent.Health.Value
02local debounce = false
03function onHit()
04if debounce == false then
05    debounce = true
06    health = health - 10
07    print (health)
08    wait(3)
09    debounce = false
10    script.Parent:Destroy()
11    end
12end
13 
14script.Parent.Touched:connect(onHit)

I made this script so that it subtracts 10 from the NumberValue (500), changing it to 490. When the number value is printed through the script, the console says 490, as it should. But when I go into the workspace and look at the actual Value of the NumberValue it still says 500. Shouldn't that change too when the part gets hit?

1 answer

Log in to vote
0
Answered by
Tomstah 401 Moderation Voter
7 years ago
Edited 7 years ago

The reason for this is because you're actually storing the value of health, not health. When you write:

1health = health.value
2health = health - 10

That only changes the value of the variable, not the object's value. You can however do:

1health = health
2health.value = health.value - 10

Which simply stores the object's address, and then changes the value directly.

0
It's a normal script, and also FE isn't enabled which adds to my confusion GlobeHousee 50 — 7y
0
Okay figured it out, gimme a bit to reedit it. Tomstah 401 — 7y
0
Whoa, it works now. Thanks for the answer GlobeHousee 50 — 7y
0
No problem man, I had a major problem with that when I first learned. You get used to it though ; P Tomstah 401 — 7y
Ad

Answer this question