I’m wondering if this AFK detection script that I made will degrade server performance. I wonder this because every second it is loop through each of the players’ tables, AND Constantly adding rbx script events to the same humanoid. Is it going to slow down the performance?
Will this AFK detection script degrade performance? If so how can I Improve it
Heres the script:
local PlayersServ = game:GetService("Players")
local Players = {}
PlayersServ.PlayerAdded:Connect(function(Player)
local AfkTable = { }
AfkTable.Player = Player
AfkTable.NoMovementTime = 0
local AfkBool = Player:WaitForChild("Afk")
local ResetFunction = function()
repeat
local Character = Player.Character
if Character then
if Character:FindFirstChild("Humanoid") then
Character.Humanoid.Running:Connect(function()
if Player:FindFirstChild("Afk") then
if Player.Afk.Value then
AfkTable.NoMovementTime = 0
AfkBool.Value = false
end
end
end)
end
end
wait()
until not Player.Parent
end
local ResetfunctionC = coroutine.wrap(ResetFunction)
ResetfunctionC()
Players[Player.Name] = AfkTable
end)
PlayersServ.PlayerRemoving:Connect(function(Player)
Players[Player.Name] = nil
end)
while true do
for _, AfkTable in pairs(Players) do
if AfkTable.NoMovementTime >= 10 then
if AfkTable.Player:FindFirstChild("Afk") then
if not AfkTable.Player.Afk.Value then
AfkTable.Player.Afk.Value = true
end
end
else
AfkTable.NoMovementTime += 1
end
wait()
end
wait(1)
end