AFK Text doesn't appear when Player isn't WindowFocused

Hey so I want to display a TextLabel with the word “AFK” and a Timer that adds up for the amount of time the Player is AFK from the game but I had this issue where the text doesn’t change it’s visible value to true when you leave the Roblox window.

Addition of something I also wanted to is adding a timer that would be displayed in the same TextLabel so it would go like “AFK: 0:00” how can I accomplish this by putting a string and a number in the same TextLabel, I’ll preview the Overhead below.

Client

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

local afkEvent = ReplicatedStorage:WaitForChild("AFKEvent")

local function focusGained()
	afkEvent:FireServer(false)
end

local function focusReleased()
	afkEvent:FireServer(true)
end

UserInputService.WindowFocused:Connect(focusGained)
UserInputService.WindowFocusReleased:Connect(focusReleased)

Server

local ReplicatedStorage = game:GetService("ReplicatedStorage")

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

local function setAfk(player, afk)
	if afk then 
		local overhead = ReplicatedStorage:WaitForChild("BillboardGui")
		overhead.Background.AFK.Visible = true
		print("AFKEvent has been called")
	end
end

afkEvent.OnServerEvent:Connect(setAfk)
2 Likes

Rather than doing

if afk then

end

You could do something like-

local overhead = --path to overhead
overhead.Background.AFK.Visible = afk -- Since afk is either true/false it'll automatically set it

Not too sure about why it isn’t showing but if you want something like the timer to display for how long they were AFK, you could go about something like this-

local function focusGained()
	afkEvent:FireServer(false, tick())
end

local function focusReleased()
	afkEvent:FireServer(true, tick())
end

SERVER

local function format_()
	local minute = math.round(diff/60)
	local second = math.ceil(diff % 60)
	if minute < 1 then
		minute = 0
	end
	return (minute..":"..second)
end

local function setAfk(player, afk, Time_)
	local overhead = ReplicatedStorage:WaitForChild("BillboardGui")
	overhead.Background.AFK.Visible = afk
	print("AFKEvent has been called")
	task.spawn(function() 
		while afk do
			local diff = math.ceil(tick() - Time_)
			overhead.Background.AFK.TextLabel.Text = format_(tostring(diff))  -- Change the thing to your path or the text which needs to be updated.
            task.wait(1)
		end
	end)
end

To find the time the user has spent AFK. You can use the tick() function. It’ll return the unix epoch time (total seconds passed since Jan 1st 1970). It gets the time at which the user left the window and each second it gets the tick() and subtracts it from the time the user has left the window. Which will always return 1+ the total time, assuming the total time starts at 0.

Then the format_() function formats it and returns the values. Minute is the total seconds elapsed divided by 60 which need not always be a whole number due to seconds being included. So the remainder of dividing it by 60 will give you the seconds ("%").