How To Make a Play Time

I need help making a play time that shows how long a player is playing for in leaderstats.
I need help to make the seconds into minutes when the go over 60 and so on. How do I do this?

This script is the leaderstats and the seconds giver.

game.Players.PlayerAdded:Connect(function(plr)
	local folder = Instance.new("Folder", plr)
	folder.Name = "leaderstats"
	
	local A = Instance.new("IntValue", folder)
	A.Name = "Orbs"
	A.Value = 0
	
	local B = Instance.new("IntValue", folder)
	B.Name = "Donated"
	B.Value = 0
	
	local C = Instance.new("IntValue", folder)
	C.Name = "PT"
	C.Value = 0
	
	while task.wait(1) do
		C.Value = C.Value +1
	end
	
end)
2 Likes

Use task.wait(), it’s better.

2 Likes

I want there to be seconts but when it hits 60 it changes to 1 minute. I am trying to abbevaite it

1 Like

Alright so after a bit of testing, I’ve figured it out.

First you’ll need to change the PlayTime leaderstat to a StringValue in order to include the abbreviation.

Then you’ll have to use string concatenation in order to update the number and the abbreviation.

To fix your script, you’ll need to add a variable called seconds and set it equal to zero which you will then increment inside your while loop. Create a variable called “minutes” that correctly converts the number of seconds into minutes (shown below).

Then inside the loop, add an if statement that checks when minutes is greater than or equal to 1. Then inside the if statement, change the string concatenation of the string value to calculate minutes instead of seconds along with the new abbrevation:

    local C = Instance.new("StringValue")
	C.Name = "PT"
	C.Parent = folder

    local seconds = 0

    while task.wait(1) do
		seconds += 1 -- increments the seconds variable
		local minutes = math.floor(seconds / 60) -- variable that calculates minutes
		
		if minutes >= 1 then -- check when minutes changes 1 or greater
			C.Value = `{minutes}m` -- change the string concatenation 
		else 
			C.Value = `{seconds}s`
		end
		
	end

Final Script:

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	local folder = Instance.new("Folder")
	folder.Name = "leaderstats"
	folder.Parent = player

	local A = Instance.new("IntValue")
	A.Name = "Orbs"
	A.Value = 0
	A.Parent = folder

	local B = Instance.new("IntValue")
	B.Name = "Donated"
	B.Value = 0
	B.Parent = folder
	

	local C = Instance.new("StringValue")
	C.Name = "PT"
	C.Parent = folder
	
	local seconds = 0
	
	while task.wait(1) do
		seconds += 1
		local minutes = math.floor(seconds / 60)
		
		if minutes >= 1 then
				C.Value = `{minutes}m`
		else
			C.Value = `{seconds}s`
		end
		
	end

end)

1 Like

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