How would I detect a player being afk for 2 minutes?

Hello, I want it if so the player is afk for over 2 minutes they will fall asleep, but as soon as they move or jump, etc. they stop.
Example:

Is there any way to achieve this?

1 Like

Skip to 6:03 when he wakes up, whenever he does an action he wakes up.

local UIS = game:GetService("UserInputService")

AFKTimer = 0

UIS.InputBegan:Connect(function()
	AFKTimer = 0
end)

while true do
	
	AFKTimer += 1
	
	wait(1)
	
	if AFKTimer == 120 then
		
		--LoadAnimation 
		
		repeat wait() until  AFKTimer == 0
		
		-- Remove Animtion
		
	end
	
end

Maybe this will work?

(Edit: just added the cancel animation bit aswell)

4 Likes

These two lines work better reversed; otherwise, solution will work well.

1 Like

Lemme try…
Charactersss
Lol

I detects when the player clicks.
I jump want if they move or jump.

game.Players.Player.Idled:Connect(function(time)
– put any code you want
end)

or you can learn more about it in Player | Roblox Creator Documentation

2 Likes

wow, never knew this function existed.

Much better than my solution XD

1 Like

I did not to i thought the only way was if the mouse moved

1 Like

So uh, how do you detect when they stop idling.

Detect when the Humanoid is moving using the Humanoid.Running event, reference the amount of time that’s given in the parameter, check if the Player’s Time is greater than 120 then do something:

--This should belong in a Server Script inside ServerScriptService
game.Players.PlayerAdded:Connect(function(Player)
    local Idle = false

    Player.Idled:Connect(function(TimeIdled)
        Idle = true
        print(Player.Name.." has been idle for: ", TimeIdled, "seconds")
        if TimeIdled > 120 and Idle == true then
            --Do stuff here
        end
    end)

    Player.CharacterAdded:Connect(function(Character)
        local Humanoid = Character:WaitForChild("Humanoid")

        Humanoid.Running:Connect(function()
            Idle = false
        end)

    end)
end)
2 Likes

Man I wish I could 2 solutions both of your guys scripts work.

1 Like