I added an AFK feature for my overheads but I keep on getting an error that I can’t fix for some reason.
It works in game but it’s just this error.
Server
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")
local afkEvent = Instance.new("RemoteEvent")
afkEvent.Name = "AfkEvent"
afkEvent.Parent = ReplicatedStorage
local afkStartTime = {}
local afkCoroutines = {}
local function setAfk(player, afk)
if not player then return end
local overhead = player.Character:WaitForChild("HumanoidRootPart"):WaitForChild("OverheadGui")
local overheadText = overhead.Background:WaitForChild("AFK")
if afk then
afkStartTime[player.UserId] = os.time()
afkCoroutines[player.UserId] = coroutine.wrap(function()
while afk do
local seconds = os.time() - afkStartTime
local minutes = math.floor(seconds / 60)
local seconds = seconds % 60
overheadText.Text = string.format("AFK: %02d:%02d", minutes, seconds)
wait(1)
end
end)
afkCoroutines[player.UserId]()
else
afkStartTime[player.UserId] = nil
if afkCoroutines[player.UserId] then
afkCoroutines[player.UserId] = nil
overheadText.Text = ""
print("Player is not AFK")
end
end
end
afkEvent.OnServerEvent:Connect(setAfk)
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 FocusedReleased()
afkEvent:FireServer(true)
end
UserInputService.WindowFocused:Connect(focusGained)
UserInputService.WindowFocusReleased:Connect(FocusedReleased)
print("AFK Complete")