Respawning a user if they do not move for more than 2 minutes

Hi!

How could I respawn a user who does not move their character for more than 2 minutes? I’m wanting to achieve this because my project being one where you can go AFK on a zone for free cash, I want the user to be respawned after 2 minutes if they don’t move essentially.

1 Like

You can use Humanoid:GetState() and check if the Humanoid is Enum.HumanoidStateType.Idle. And everytime the Humanoid walks, you can reset the 120 seconds timer, you should probably use an IntValue to save it and whenever someone stands still; start to subtract 1 by every second.

I have to say that using an IntValue is very ineffective. You could just use a variable, or a table if you need to manage many players at a time, and then use a for loop to loop through them every second.

This sounds perfect for you, Player Idled event

This event is usually fired two minutes after the game engine classifies the player as idle.

If you only care if they haven’t moved their character for two minutes (but they’ve moved their camera) then you can reset a timer anytime the player uses a PlayerAction with BindAction.

EDIT:

The Idled event probably won’t fire? Also there are some alternative methods in there for checking if someone is idle.

1 Like

One simple solution is to check if a player’s position hasn’t been moved in two minutes. You shouldn’t check for a definite equals since it’s possible for there to be rounding errors:

-- haven't coded in a while, but this should be generally right
local playerPos = {}
local maxIdleTime = 2*60

while (true) do
    wait(1)
    for _,plyr in pairs(game.Players:GetPlayers()) do
        local cpos = plyr.Character.HumanoidRootPart.Position
        if playerPos[plyr] == nil then
            playerPos[plyr] = {cpos,tick()}
        elseif (cpos - playerPos[plyr][1]).magnitude < 1 and (tick() - playerPos[plyr][2]) > maxIdleTime then
             plyr.Character.Humanoid:BreakJoints()
             // or
             // plyr:LoadCharacter() 
        end
        playerPos[plyr][0] = cpos
    end
end

Another option is to have the client check for any mouse/keyboard inputs. If nothing was done after two minutes, tell the server to respawn the player.

1 Like