I am making an afk script and I am trying to get it to output to the chat when a player goes afk and when the player returns but it seems to output the text many times. Can someone fix this?
Note: This is something someone whipped up for me a long time ago and I am getting back into lua so I am still relearning.
[lua]
AwayTime = 300 // seconds. (300 = 5 min)
function AFK()
for _, v in pairs( player.GetAll()) do
local a = (v:GetNWInt(“LA”) + AwayTime) - CurTime()
if a <= 0 then
v:SetNWBool(“AFK”, true)
if v:GetNWBool(“AFKN”) == true then
for k, ply in pairs( player.GetAll( ) ) do
ply:PrintMessage( HUD_PRINTTALK, v:Nick() … " is now AFK." )
v:SetNWBool(“AFKN”, false)
end
end
end
end
end
timer.Create(“AFK_timer”, 1, 0, AFK)
function ResetAFK(ply)
if ply:GetNWBool(“AFK”) == true then
for _, v in pairs( player.GetAll()) do
v:PrintMessage( HUD_PRINTTALK, ply:Nick() … " is no longer AFK." )
end
end
ply:SetNWInt(“LA”, CurTime())
ply:SetNWBool(“AFK”, false)
ply:SetNWBool(“AFKN”, true)
end
hook.Add( “KeyPress”, “KeyPressed”, ResetAFK )
hook.Add( “KeyRelease”, “KeyReleased”, ResetAFK )
hook.Add( “PlayerInitialSpawn”, “PlayerInitialSpawn”, ResetAFK )
[/lua]