How would I make a script to trigger off of players inactivity?

I want to damage players slowly for being AFK and have no clue how to check for them not moving around. I am looking specifically for something that checks movement of the character not the mouse.

You could use these functions: WindowFocusReleased - WindowFocused - Idled

2 Likes

Check their displacement every frame and if it’s less than one (player is not moving), build up a timer using the deltaTime of Heartbeat. If the time buildup exceeds a certain amount and the player’s displacement from their last position is still less than one then start damaging them slowly.

RunService.Heartbeat(deltaTime)
    if displacement of player's position from last frame <= 1
    playerDeltaBuildup += deltaTime
    if playerDeltaBuildup >= thresholdForBuildup
        hurtPlayer(damageAmount * deltaTime)

This is for a secure server-sided approach. Person who posted before me posted signals that are only useable by the client and an exploiter can easily bypass those checks and avoid getting damaged while not moving.

1 Like

i’d use humanoid.movedirection. heres a simple script

-- server script in server script service
local TimeUntilDamage = 5 -- how long they have to be afk for it to start damaging them
local Damage = 10 -- damage it does
game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Char)
		local AfkTime = 0
		local Humanoid:Humanoid = Char:WaitForChild("Humanoid")
		Humanoid.Changed:Connect(function(P)
			if P == "MoveDirection" then
				if Humanoid.MoveDirection == Vector3.new(0,0,0) then
					while Humanoid.MoveDirection == Vector3.new(0,0,0) do
						AfkTime += 1
						if AfkTime >= TimeUntilDamage then
							Humanoid.Health -= Damage
						end
						task.wait(1)
					end
				else
					AfkTime = 0 -- sets back to 0 since the player started to move
				end
			end
		end)
	end)
end)

@taylornoni

1 Like

Exactly what I was looking for thank you!

np glad I could help

1 Like