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.
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.
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.
--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.