AFK Timing System

Hey Developers!

I’m trying to make some sort of timing system so when a user goes off the roblox tab that it start’s a sort of timer that adds the time to a value. So for example if someone goes off the Roblox tab for 30 seconds then the value is equal to 30 seconds.

I’ve looked around the forums and seen things a bit like this with values and printing when player’s leave and join a game but I’m not sure how to make it compatible for what I’m trying to do.

Any help would be appreciated :heart:

1 Like

Im unaware of a way to detect if someone is off the roblox tab, but I know of the Player.Idled event, which triggers when roblox deems the player has been inactive for two minutes.

1 Like

You can use WindowFocusReleased to determine when someone tabs out of Roblox, as well as WindowFocused to determine when someone tabs into Roblox. If you need more help from here, let me know. :slight_smile:

2 Likes

Yeah thanks for that, it helped a bit. :slight_smile:

I’m still having a problem with my main script though I’ll leave the script below. If you could help me that would be most appreciated.

Local script:

local UIS = game:GetService("UserInputService")

UIS.WindowFocusReleased:Connect(function()
	game.ReplicatedStorage.AFK:FireServer(true)
end)

UIS.WindowFocused:Connect(function()
	game.ReplicatedStorage.AFK:FireServer(false)
end)

Server Script:

game.ReplicatedStorage.AFK.OnServerEvent:Connect(function(Player)
	local Timer = Instance.new("IntValue", Player)
	Timer.Name = "AFKTime"
	while wait(1) do
		Timer.Value = Timer.Value + 1
	else
	Timer.Value = Timer.Value + 0
       end
	end
end)
--SERVER

local players = game:GetService("Players")
local replicated = game:GetService("ReplicatedStorage")
local afkRemote = replicated.AFKRemote

local playerStates = {}

local function onPlayerAdded(player)
	local ls = Instance.new("Folder")
	ls.Name = "leaderstats"
	ls.Parent = player
	
	local afk = Instance.new("IntValue")
	afk.Name = "AFK"
	afk.Parent = ls
end

local function onPlayerRemoving(player)
	playerStates[player] = nil
end

local function onAfkRemoteFired(player, state)
	if state then
		playerStates[player] = true
		while playerStates[player] do
			task.wait(1)
			player.leaderstats.AFK.Value += 1
		end
	else
		playerStates[player] = false
	end
end

afkRemote.OnServerEvent:Connect(onAfkRemoteFired)
players.PlayerAdded:Connect(onPlayerAdded)
players.PlayerRemoving:Connect(onPlayerRemoving)
--LOCAL

local userInput = game:GetService("UserInputService")
local replicated = game:GetService("ReplicatedStorage")
local afkRemote = replicated:WaitForChild("AFKRemote")

local function onWindowFocused()
	afkRemote:FireServer(false)
end

local function onWindowReleased()
	afkRemote:FireServer(true)
end

userInput.WindowFocused:Connect(onWindowFocused)
userInput.WindowFocusReleased:Connect(onWindowReleased)

Single RemoteEvent instance required named “AFKRemote” and placed inside the ReplicatedStorage container.

3 Likes

Thank’s so much! This really helped. :slight_smile: