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
local health = script.Parent.Parent.Health.Value
local debounce = false
function onHit()
if debounce == false then
    debounce = true
    health = health - 10
    print (health)
    wait(3)
    debounce = false
    script.Parent:Destroy()
    end
end

script.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:

health = health.value
health = health - 10

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

health = health
health.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