01 | local health = script.Parent.Parent.Health.Value |
02 | local debounce = false |
03 | function onHit() |
04 | if 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 |
12 | end |
13 |
14 | 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?
The reason for this is because you're actually storing the value of health, not health. When you write:
1 | health = health.value |
2 | health = health - 10 |
That only changes the value of the variable, not the object's value. You can however do:
1 | health = health |
2 | health.value = health.value - 10 |
Which simply stores the object's address, and then changes the value directly.