Afk timer issue

I am trying to make a afk timer like what you get at grp

here is my code:

Code
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local AfkEvent = Instance.new("RemoteEvent")
AfkEvent.Name = "AfkEvent"
AfkEvent.Parent = ReplicatedStorage

local function setAfk(player, afk)
	if afk then
		print("afk")
		local time1 = 0
	
		
		local Nametag = player.Character.Head.nametag.afk
		local time1 = 0
		print("reset")
		Nametag.Visible = true
		
		if Nametag.Visible == true then
			repeat 
				print(1)
				wait(1)
				time1 = time1 + 1
				Nametag.Text = "[AFK " .. time1 .. "]"
			until afk == false
		end
		
		
	else
		local Nametag = player.Character.Head.nametag.afk
		Nametag.Visible = false
		Nametag.Text = "[AFK]"
		end
	end

AfkEvent.OnServerEvent:Connect(setAfk)

The issue is that it keeps bugging out when i got afk then go back to playing then go afk agan.
Please let me know if you know a fix.

This is now fixed, here is the working script.

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local AfkEvent = Instance.new("RemoteEvent")
AfkEvent.Name = "AfkEvent"
AfkEvent.Parent = ReplicatedStorage

local function setAfk(player, afk)
	if afk then
		print("afk")
		local time1 = 0
	
		
		local Nametag = player.Character.Head.nametag.afk
		local Nametag1 = player.Character.Head.nametag.afktag
		local time1 = 0
		print("reset")
		Nametag1.Visible = true
		Nametag.Visible = true
		
		if Nametag.Visible == true then
			time1 = 1
			repeat 
				print(time1)
				wait(1)
				time1 = time1 + 1
				
				if time1 < 60 then
					Nametag.Text = time1 .. "s"
				end
				
				if time1 >= 60 then
					local timemin = time1 - 60
					Nametag.Text = "1m " .. timemin .. "s"
				end
				
				if time1 >= 120 then
					local timemin1 = time1 - 120
					Nametag.Text = "2m " .. timemin1 .. "s"
				end
				
				if time1 >= 180  then
					local timemin2 = time1 - 180
					Nametag.Text = "3m " .. timemin2 .. "s"
				end
				
				if time1 >= 240  then
					local timemin3 = time1 - 240
					Nametag.Text = "4m " .. timemin3 .. "s" 
				end
				
				if time1 >= 300 then
					local timemin4 = time1 - 300
					Nametag.Text = "5m " .. timemin4 .. "s"
				end
				
				if time1 >= 360  then
					local timemin5 = time1 - 360
					Nametag.Text = "6m " .. timemin5 .. "s"
				end
				
				if time1 >= 420 then
					local timemin6 = time1 - 420
					Nametag.Text = "7m " .. timemin6 .. "s"
				end
				
				if time1 >= 480 then
					local timemin7 = time1 - 480
					Nametag.Text = "8m " .. timemin7 .. "s"
				end
				
				if time1 >= 540 then
					local timemin8 = time1 - 540
					Nametag.Text = "9m " .. timemin8 .. "s"
				end
				
				if time1 >= 600 then
					local timemin9 = time1 - 600
					Nametag.Text = "10m " .. timemin9 .. "s"
				end
				
				if time1 >= 660  then
					player:kick("Idle over 11 Min, Please Reconnect!")
				end
				
			until Nametag.Visible == false
		end
		
		
	else
		local Nametag1 = player.Character.Head.nametag.afktag
		local Nametag = player.Character.Head.nametag.afk
		Nametag.Visible = false
		Nametag1.Visible = false
		Nametag.Text = "s"
		end
	end

AfkEvent.OnServerEvent:Connect(setAfk)
1 Like