Help on a timer script

Hi there, I’m new to development so i’m still learning simple code.
I would like to create a timer on the leaderboard where after touching a certain block (part), the timer starts and when the player dies, their timer resets back to 0 seconds until they touch the certain block again.

This is the code i have:

game.Players.PlayerAdded:connect(function(player)
local Stats = Instance.new(“Model”, player)
Stats.Name = “leaderstats”
local Timer = Instance.new(“IntValue”, Stats)
Timer.Name = “Time”
Timer.Value = 0
while true do
wait(1)
Timer.Value = Timer.Value +1
end
end)

Could i get some help to add those features.

2 Likes

Make your “leaderstats” as a folder, not a model.

As above has mentioned, leaderstats should be a Folder, not a model.
You can use player.CharacterAdded to get the character and it’s humanoid to connect the humanoid.Died function, which fires when a player’s character dies.
But you can also simply add this in your while loop:

 while wait(1) do
    if player.Character and player.Character:FindFirstChildOfClass("Humanoid").Health == 0 then
          Timer.Value = 0
    else
          Timer.Value = Timer.Value + 1
    end
 end

Here is the correct script you should use if you originally want to use to the while wait System:

game.Players.PlayerAdded:connect(function(player)
local Stats = Instance.new(“Folder”, player)
Stats.Name = “leaderstats”
local Timer = Instance.new(“IntValue”, Stats)
Timer.Name = “Time”
Timer.Value = 0
while true do
wait(1)
Timer.Value = Timer.Value +1
end
end)

Should I also use the if script to make the timer start when touching a specific part?

In regards to the Timer Reset when Character touch’s a part, do you want the character to load or “oof”?

script.Parent.Touched:Connect(function(Hit)
if Hit then
if Hit.Parent then
local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
if Player then
Player.Character.HumanoId:FindFirstChildOfClass(“Humanoid”).Health = 0
Player.leaderstats.Time.Value = 0
end
end
end
end)

or

script.Parent.Touched:Connect(function(Hit)
if Hit then
if Hit.Parent then
local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
if Player then
Player.Character.HumanoId:FindFirstChildOfClass(“Humanoid”):LoadCharacter()
Player.leaderstats.Time.Value = 0
end
end
end
end)

I meant the timer starts when I touch a certain part, a bit like what the yellow bit on speed run 4 does, and the timer would reset when I die

I believe this is what you were asking for
Throw this inside a script parented to the part you want touched

local part = script.Parent

local touchedplayers = {}
local debounce = false

-- you may remove the prints, kept them in to make it easier to understand the script
part.Touched:Connect(function(hit)
	if game.Players:GetPlayerFromCharacter(hit.Parent) then
		if debounce == false then -- this makes it so that the touched event doesn't run repeatedly
			print('touched')
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)
			debounce = true
			
			if player.leaderstats:FindFirstChild("Timer") == nil then	-- creates timer if one doesn't already exist
				print('timeradded')
				local timer = Instance.new("NumberValue")
				timer.Name = "Timer"
				timer.Parent = player.leaderstats
			end
			
			if player.leaderstats:FindFirstChild("Timer")  then -- checks for timer and if the player is added to the table, if true then the timer starts
				if table.find(touchedplayers,player) == nil then 
					table.insert(touchedplayers,player)
					local timer = player.leaderstats.Timer
					timer.Value = 0
					coroutine.resume(coroutine.create(function() -- coroutine allows the while wait(1) do loop to continue running without stopping the script, allowing it to change the debounce value again
						while wait(1) do
							if hit.Parent.Humanoid.Health < 1 then table.remove(touchedplayers,table.find(touchedplayers,player)) end
							timer.Value = timer.Value + 1
							print(timer.Value)
							if table.find(touchedplayers,player) == nil then print('break') coroutine.yield() end -- 
						end
					end))
				end
			end
			
			if player.leaderstats:FindFirstChild("Timer") and player.leaderstats:FindFirstChild("Timer").Value > 0 then
				table.remove(touchedplayers,table.find(touchedplayers,player)) -- once the player is removed from the table, it stops counting, timer is not reset until started again
				print('stopping')
			end
			
			wait(1)
			debounce = false
		end
	end
end)
3 Likes

Can i ask which bit of this script would be the part that resets my character after i die? As i had tried using this script and the leaderboard didn’t show up.