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?
function onTouched(part) local h = part.Parent:findFirstChild("Humanoid") local timeRemaining = 0 local seconds = game.ReplicatedStorage.Seconds.Value seconds = 0 if h~=nil then script.Parent.Position = Vector3.new(-12.15, 621.894, 47.98) script.Parent.Parent:FindFirstChild("Start").SurfaceGui.SIGN.Text = "3" wait(1) script.Parent.Parent:FindFirstChild("Start").SurfaceGui.SIGN.Text = "2" wait(1) script.Parent.Parent:FindFirstChild("Start").SurfaceGui.SIGN.Text = "1" wait(1) script.Parent.Parent:FindFirstChild("Start").SurfaceGui.SIGN.Text = "GO!" wait(0.5) script.Parent.Parent:FindFirstChild("Start").Transparency = "1" script.Parent.Parent:FindFirstChild("Start").CanCollide = false script.Parent.Parent:FindFirstChild("Start").SurfaceGui.SIGN.TextTransparency = 1 timeRemaining = 300 while timeRemaining > 0 do wait(1) timeRemaining = timeRemaining - 1 seconds = seconds - 1 script.Parent.Parent:FindFirstChild("Water").Position = script.Parent.Parent:FindFirstChild("Water").Position + Vector3.new(0,0.1,0) end wait(30) script.Parent.Position = Vector3.new(-12.15, 21.894, 47.98) script.Parent.Parent.Water.Position = Vector3.new(-12.15, -855.409, -1.52) script.Parent.Parent:FindFirstChild("Start").Transparency = "0" script.Parent.Parent:FindFirstChild("Start").CanCollide = true script.Parent.Parent:FindFirstChild("Start").SurfaceGui.SIGN.TextTransparency = 0 timeRemaining = 0 end end script.Parent.Touched:connect(onTouched)
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:
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:
seconds = 0 -- snip 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:
local seconds = game.ReplicatedStorage.Seconds -- Grab reference to IntValue object seconds.Value = 0 -- modify the IntValue object -- snip seconds.Value = seconds.Value - 1 -- Alternatively, this is now possible on Roblox: -- seconds.Value -= 1