Timer reset issue

  1. What do you want to achieve? Keep it simple and clear!

I currently have a timer system. I have a remote event named OneMinuteTimer in a folder called Remotes in ReplicatedStorage. I also have a folder full of CheckPoint parts in workspace. Once the player touches one of the checkpoints, a remote event will fire and start a timer count down starting from 1:00.

  1. What is the issue? Include screenshots / videos if possible!

Here is a video with the issue. As you can see, once the player touches the first part everything runs the way it should. Once the player touches the part their timer is set to 60 and starts counting down, and if they touch the same part again nothing happens. But if they then go to touch a second part, the timer starts flickering between two different set times. I want it so that once they touch the second part, their timer will be reset to 60 and then just start counting down from there. (Ignore the background):

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

Yes I have looked around but could not find anybody with a similar issue to mine.

Local script in StarterPlayerScripts connected to remote event:

local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("OneMinuteTimer") 
local Checkpoint = game.Workspace.Checkpoints

local function check()
	local value = Player.leaderstats.Stage.Value
	Player.Character:MoveTo(Checkpoint[value+1].Position)
end

local function remove()
	for _, tool in ipairs(Player.Backpack:GetChildren()) do
		if tool:IsA("Tool") then
			tool:Destroy()
		end
	end 
	if Player.Character:FindFirstChildOfClass("Tool") then
		Player.Character:FindFirstChildOfClass("Tool"):Destroy()
	end 
end

local function teleport()
	local clonedgui = Player.PlayerGui:WaitForChild("Timer")
	local minutes = 1
	local seconds = 0
	repeat
		if seconds <= 0 then
			minutes = minutes - 1
			seconds = 59
		else
			seconds = seconds - 1
		end
		if seconds < 10 then
			clonedgui.Frame.TextLabel.Text = tostring(minutes)..":0"..tostring(seconds)
		else 
			clonedgui.Frame.TextLabel.Text = tostring(minutes)..":"..tostring(seconds)
		end
		if seconds <= 0 and minutes <= 0 then
			remove()
			check()
		end
		Player.CharacterAdded:Connect(function()
			minutes = 1
			seconds = 0
		end)
		wait(1)
	until minutes <= 0 and seconds <= 0
end

remoteEvent.OnClientEvent:Connect(teleport)

I appreciate some help, thanks.

1 Like

Is the old script that’s updating the timer value still running in the background?

1 Like

I just realised that in the old post that i made i thought u meant something else, here try this script

local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("OneMinuteTimer") 
local Checkpoint = game.Workspace.Checkpoints

local function check()
	local value = Player.leaderstats.Stage.Value
	Player.Character:MoveTo(Checkpoint[value+1].Position)
end

local function remove()
	for _, tool in ipairs(Player.Backpack:GetChildren()) do
		if tool:IsA("Tool") then
			tool:Destroy()
		end
	end 
	if Player.Character:FindFirstChildOfClass("Tool") then
		Player.Character:FindFirstChildOfClass("Tool"):Destroy()
	end 
end

local function teleport()
	local clonedgui = Player.PlayerGui:WaitForChild("Timer")
	local minutes = 1
	local seconds = 0
	repeat
		if seconds <= 0 then
			minutes = minutes - 1
			seconds = 59
		else
			seconds = seconds - 1
		end
		if seconds < 10 then
			clonedgui.Frame.TextLabel.Text = tostring(minutes)..":0"..tostring(seconds)
		else 
			clonedgui.Frame.TextLabel.Text = tostring(minutes)..":"..tostring(seconds)
		end
		if seconds <= 0 and minutes <= 0 then
			remove()
			check()
		end
		Player.CharacterAdded:Connect(function()
			minutes = 1
			seconds = 0
		end)
		wait(1)
	until minutes <= 0 and seconds <= 0 or remoteEvent.OnClientEvent
end

remoteEvent.OnClientEvent:Connect(teleport)
1 Like

That just stopped the timer from counting down.

Yes, the timer function when activated will get the timer to count down until it is at 0.

Does the remote get fired multiple times (e.g. when the player reaches an checkpoint) or only one time

can’t you just remove the teleport function and have a start/reset function?

local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("OneMinuteTimer") 
local Checkpoint = workspace:WaitForChild("Checkpoints")

local function check()
	local value = Player.leaderstats.Stage.Value
	Player.Character:MoveTo(Checkpoint[value+1].Position)
end

local function remove()
	for _, tool in ipairs(Player.Backpack:GetChildren()) do
		if tool:IsA("Tool") then
			tool:Destroy()
		end
	end 
	if Player.Character:FindFirstChildOfClass("Tool") then
		Player.Character:FindFirstChildOfClass("Tool"):Destroy()
	end 
end

local clonedgui = Player.PlayerGui:WaitForChild("Timer")
local minutes = 1
local seconds = 0
Started = false

function StartTimer()
	while minutes >= 0 or seconds >= 0 do
		if seconds <= 0 then
			minutes = minutes - 1
			seconds = 59
		else
			seconds = seconds - 1
		end
		if seconds < 10 then
			clonedgui.Frame.TextLabel.Text = tostring(minutes)..":0"..tostring(seconds)
		else 
			clonedgui.Frame.TextLabel.Text = tostring(minutes)..":"..tostring(seconds)
		end
		if seconds <= 0 and minutes <= 0 then
			remove()
			check()
		end
		Player.CharacterAdded:Connect(ResetTimer)
		wait(1)
	end 
end

function ResetTimer()
  if not Started then
    Started = true
    task.spawn(StartTimer)
  else 
    minutes = 1
    seconds = 0
  end
end

remoteEvent.OnClientEvent:Connect(ResetTimer)

It gets fired once each time a different checkpoint part is touched.

Thanks for the response. That works actually. When the player touches a checkpoint part their timer will be set to 1:00 and start counting down. If they then go touch a different checkpoint part their timer will reset to 1:00 and start counting down from there. But then if the player goes back to the original checkpoint part and touches that part the timer does not reset to 1:00. I think that this has to do with the script in the checkpoint parts though,

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent:RemoteEvent = ReplicatedStorage:WaitForChild("OneMinuteTimer")


local Debounces = {}

script.Parent.Touched:Connect(function(touched)
	if touched.Parent:IsA("Model") and touched.Parent:FindFirstChild("Humanoid") then
		local Player = Players:GetPlayerFromCharacter(touched.Parent)
		if Player and not Debounces[Player] then
			Debounces[Player] = true
			remoteEvent:FireClient(Player)
		end
	end
end)

remoteEvent.OnServerEvent:Connect(function(plr)
	Debounces[plr] = nil
end)

Players.PlayerRemoving:Connect(function(plr)
	Debounces[plr] = nil
end)

probably because you’re never removing the player from debounces

Oh okay so how could I fix that.

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