I have a script inside of a model in workspace that changes an intValue to indicate how many seconds are left inside of one round located in ReplicatedStorage every time a second passes. When I test the game and touch the part that starts a round, the value doesn't change at all. Any suggestions?
01 | function onTouched(part) |
02 | local h = part.Parent:findFirstChild( "Humanoid" ) |
03 | local timeRemaining = 0 |
04 | local seconds = game.ReplicatedStorage.Seconds.Value |
05 | seconds = 0 |
06 | if h~ = nil then |
07 | script.Parent.Position = Vector 3. new(- 12.15 , 621.894 , 47.98 ) |
08 | script.Parent.Parent:FindFirstChild( "Start" ).SurfaceGui.SIGN.Text = "3" |
09 | wait( 1 ) |
10 | script.Parent.Parent:FindFirstChild( "Start" ).SurfaceGui.SIGN.Text = "2" |
11 | wait( 1 ) |
12 | script.Parent.Parent:FindFirstChild( "Start" ).SurfaceGui.SIGN.Text = "1" |
13 | wait( 1 ) |
14 | script.Parent.Parent:FindFirstChild( "Start" ).SurfaceGui.SIGN.Text = "GO!" |
15 | wait( 0.5 ) |
P.S. I'm new to scripting forums, so I may have formatted a things wrong. Sorry about that.
First, the code reads the value of the IntValue:
1 | local seconds = game.ReplicatedStorage.Seconds.Value |
Now a copy of the value is stored in this local variable.
The code then goes on to update that variable, while leaving the IntValue object itself intact:
1 | seconds = 0 |
2 | -- snip |
3 | seconds = seconds - 1 |
The issue is that seconds
doesn't actually point to the IntValue in ReplicatedStorage; it is simply a copy of the IntValue's original Value. When the code assigns to seconds
, it's kind of like imagining that the IntValue is changed - it doesn't have any effect on the IntValue itself. What you want instead is this:
1 | local seconds = game.ReplicatedStorage.Seconds -- Grab reference to IntValue object |
2 | seconds.Value = 0 -- modify the IntValue object |
3 | -- snip |
4 | seconds.Value = seconds.Value - 1 |
5 | -- Alternatively, this is now possible on Roblox: |
6 | -- seconds.Value -= 1 |