Why Does Timer Only Work Once?

Basically, I have a script that activates when a remote event is fired and it counts down from 7, however it seems like it only works once, then the timer is just permanently 0. Is there any fixes for this?

local rem = game.ReplicatedStorage:WaitForChild("RemoteEvents"):WaitForChild("RebirthEvent")
local timer = 7
local tool = game.StarterPack:WaitForChild("Stick")

rem.OnServerEvent:Connect(function(player)
	local timer = 7
	player.leaderstats.Coins.Value = 0
	player.leaderstats["Sword Skill"].Value = 0
	player.leaderstats.Multiplier.Value += 1
	while timer > 0 do
		player.Backpack:ClearAllChildren()
		timer -= 0.2
		print(timer)
		wait(0.2)
	end
	local stick = tool:Clone()
	stick.Parent = player.Backpack
end)

Remove the reference to the timer variable from outside of the OnServerEvent as you are duplicating variable names which will cause you confusion.

1 Like

Ah, thank you, this fixed it! I also just defined the timer variable in the beginning which also worked.

Be careful of declaring it outside the Connect, as you will need to ensrue it is reset back to its default value after the while loop has ended.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.