AFK Timer Second to Minutes conversion

I have a afk timer, whenever the player goes AFK a timer will begin over the head, but after 60s it will just countinue going 61s, 62s etc… How would I make it change to 1:00 once it hits 60s and countinue on.

1 Like

Well I am guessing your AFK timer works via a while true loop so all you would need to do is use an if statement to see if the seconds are 60 and if it is then turn it to 1:00 and then write like a variable or something so you know not to add the number on via just 60 but with the 1:01, 1:02, 1:03 as an example.

How would I write a statment? The script for the timer is below its in StarterCharacterScript

local Char = script.Parent
local Player = game.Players:GetPlayerFromCharacter(Char)

local AFKTag = script:FindFirstChild("AFKTag"):Clone()
AFKTag.Parent = Char:FindFirstChild("Head")
AFKTag.Adornee = Char:FindFirstChild("Head")

local Remote = script:WaitForChild("AFK")

local AFK = false

Remote.OnServerEvent:Connect(function(FiredPlayer, Value)
	if Player ~= FiredPlayer then
		return
	end
	
	AFK = Value
	
	if not AFK then
		AFKTag.Enabled = false
		return
	end
	
	AFKTag.Enabled = true
	
	local Current = 0

	while true do
		if not AFK then
			break
		end

		AFKTag.Frame.AFKSecondTimer.Text = Current.."s"
		Current += 1
		
		task.wait(1)
	end
end)

and then a local script in the script saying:

local UserInputService = game:GetService("UserInputService")

local Player = game.Players.LocalPlayer

repeat task.wait() until Player.Character

local Character = Player.Character

local Remote = script.Parent:WaitForChild("AFK")

UserInputService.WindowFocusReleased:Connect(function()

Remote:FireServer(true)

end)

UserInputService.WindowFocused:Connect(function()

Remote:FireServer(false)

end)

the text is in the script as a billboardgui aswell if your wondering

You can try using the 2 solutions in a similar post on the devforums

Both convert to HMS, but it should be easy to change around for your needs

To convert your Current into a string, use tostring

1 Like

Ignore what I said. Do what one of the posts that @EmbatTheHybrid put. There method is way more efficient then my idea.

Just if you want S just see if the minute is == to 00 and if it is just put the seconds but if it’s not == to 00 then it should be put as a minute:second.

I have found this on the linked topics

local function toHMS(s)
	return string.format("%02i:%02i:%02i", s/60^2, s/60%60, s%60)
end

How would I put this string on the text

Run the function on where you need the string, in your case

AFKTag.Frame.AFKSecondTimer.Text = toHMS(Current)

You legit just put it in or if you don’t want the whole thing you could split the string with the : and get the seconds on there own and then the minutes if you only want the minutes to show after the seconds.

Although I am happy to help it just seems like your asking us to do everything even though it’s kinda clear how to implement it.

local minutes = math.floor(seconds/60)
seconds -= minutes*60

just like that.

Am definitely wrong but I tried to get it to work

	if Current > 59 then
		AFKTag.Frame.AFKSecondTimer.Text = toHMS(Current)
	else
		wait()

i made it so every second it checks if its above 59 seconds if not it continues the script normally

but once it hits 58 the script just pauses until the player comes back

Ok I have found the solution.

if Current > 59 then
	AFKTag.Frame.AFKSecondTimer.Text = toHMS(Current)
            wait(1)
else
	wait()

I needed to add a wait after the 2nd line because it said “executed too fast” thank you everyone who helped me make this simple 5 lines of code.

You should mark the post that gave you the `toHMS function as solution, which according to you was the post by @EmbatTheHybrid that gave you the link to your solution

That’s true, thanks.

ignore this