Time played on game

I have tried with fire events but it is too confusing and I have tried every way I can think of to fix it but I dont know how. I want to have a time played leaderstat but I want it to start when a player touches a button and for it to end when they die but I have no idea how to get this to work any ideas?

1 Like

When the player joins, start a timer and wait until the player leaves.

local plrtimestarts: {[Player]: number} = {}
local plrs = game:GetService("Players")

plrs.PlayerAdded:Connect(function(plr)
plrtimestats[plr] = os.clock()
end)

plrs.PlayerRemoving:Connect(function(plr)
local plrtime = os.clock() - plrtimestats[plr]
--Save to datastore/leaderstats here
end)
1 Like

I dont want it like that though (I mean this as nicely as I can) I want it so that when a player actually starts playing the game the timer starts and when they die the timer stops then to see if that time was better than the last and make a best time leaderboard.

I know, I was just making another reply because I just noticed it. Sorry for not reading your post properly

1 Like

Create a touched event on a block and have a table of all the players who touched it so they can’t touch it again then every player that touches it create a died event for them so when they die it will get the time touched to the time died then remove them and compare the times

Sorry in a rush atm

local plrtimestarts: {[Player]: number} = {}
local plrs = game:GetService("Players")

part.Touched:Connect(function(plr)
if plrtimestarts[plr] then return end
plrtimestarts[plr] = os.clock()
end)

plrs.PlayerAdded:Connect(function(plr)
local char = plr.Character or plr.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")
humanoid.Died:Connect(function()
local plrtime = os.clock() - plrtimestarts[plr]
--Do stuff
end)
end)

So this would be in a normal script under that part touched?

Yes, and you also need a variable named part which will be that part. At the part where I commented “Do stuff”, you should do what you are going to do with the time (inserting to a leaderboard)

image
The underlined bits I do not know how to fix?

Edited the code, try that one
(We can’t even say 30 “letters” anymore?)

You made a few spelling mistakes. The first two lines need an R and the third one should have plr, not player.

so do stuff will check if the time is better than the one already in the leaderboard and if it is change it?

Yes

(30 30 30 30 30 30 30 30 30)

1 Like

attempt to perform arithmetic (sub) on number and nil
local plrtime = os.clock() - plrtimestarts[plr]

Oh, I forgot that the touched event has a part as the parameter. Replace script.Parent.Touched… part with:

script.Parent.Touched:Connect(function(part)
local plr = plrs:GetPlayerFromCharacter(part:FindFirstAncestorOfClass("Model"))
if (not plr) or plrtimestarts[plr] then return end
plrtimestarts[plr] = os.clock()
end)

can you help me it says attempt to index nil with 'findFirstChild'

		print(plrtime)
		local Player = game.Players:findFirstChild(humanoid.Name)
		local Stats = Player:findFirstChild("leaderstats")
		local BestTime = Stats:findFirstChild("BestTime")
		print(BestTime)
		if BestTime.Value<plrtime then
			BestTime.Value = plrtime

Replace this line with:

local Player = game.Players:GetPlayerFromCharacter(humanoid.Parent)

sorry one last thing and ill be done
The script only plays once how do I make it so every time that the player steps on the part then it will restart the timer and stop it when it is dead.

If you want to restart the timer then the Touched event should be replaced with this:

local debounce = false
script.Parent.Touched:Connect(function(part)
if debounce then return end
local plr = plrs:GetPlayerFromCharacter(part:FindFirstAncestorOfClass("Model"))
debounce = true
plrtimestarts[plr] = os.clock()
task.wait(0.2)
debounce = false
end)

when the player starts the game call the StartTimer funtion and when they die call the StopTimer function the StopTimer function will return the amount of time the player took

local timers = {}

local function StartTimer(player)
	timers[player] = os.clock()
end

local function StopTimer(player)
	if timers[player] == nil then return end
	local deltaTime = os.clock() - timers[player]
	timers[player] = nil
	return deltaTime
end

game.Workspace.Button.Touched:Connect(function(part)
	local player = game.Players:GetPlayerFromCharacter(part.Parent)
	if player == nil then return end
	StartTimer(player)
end)

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		character:WaitForChild("Humanoid").Died:Connect(function()
			local deltaTime = StopTimer(player)
			if deltaTime == nil then return end
			print(player, deltaTime)
		end)
	end)
end)

-- to prevent memory leaks when the player exits the game make sure to remove them from the timers table
game.Players.PlayerRemoving:Connect(function(player)
	timers[player] = nil
end)