Hey devforum how can I make it so if the player stands still for a certain ammount of time they will go afk?

As the title suggests I want to make it so that if a player stands still for lets say 5 seconds without moving then they it will print("1") how can I do this? My current attempt is using a movement module I made for detecting movement and using spawn functions waits and denounces but as you can tell that didn’t go to plan.

here’s my movement detector in bind to render stepped;

if not MOVEMENT_FUNCTIONS.CheckMovement() then
		
end;

returns false if not moving returns true if is moving.

1 Like
local players = game:GetService("Players")

player.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		player.Idled:Connect(function(duration)
			if duration >= 60 then --60 seconds
				--do code
			end
		end)
	end)
end)

https://developer.roblox.com/en-us/api-reference/event/Player/Idled

1 Like

This would not work because I want to detect small increments of time like 5 seconds. In the documentation it says it will be fired 2 mins after the engine detects player to be afk which is way too long for me.

1 Like

Okay, to be fair you did clarify that in the post, I only read the title.

local players = game:GetService("Players")
local player = players.LocalPlayer or players.PlayerAdded:Wait()
local uis = game:GetService("UserInputService")
local timer = 0
local idle = false

uis.InputBegan:Connect(function()
	timer = 0
	idle = false
end)

uis.InputEnded:Connect(function()
	while task.wait(1) do
		timer += 1
		if timer >= 5 then
			idle = true
			--do stuff
			break --break out of loop when finished
		end
	end
end)
1 Like

Won’t this code indefinitely yield for 5 seconds even after InputBegan fired (if the player moved in the 5 second duration)? Add a check in the while loop to determine if InputBegan happenned to break out the loop without doing anything or use events

1 Like
break --break out of loop when finished

I left the customisation up to the original poster.

local players = game:GetService("Players")
local player = players.LocalPlayer or players.PlayerAdded:Wait()
local uis = game:GetService("UserInputService")
local timer = 0
local idle = false

uis.InputBegan:Connect(function()
	timer = 0
	idle = false
end)

uis.InputEnded:Connect(function()
	idle = true
	while task.wait(1) do
		if not idle then
			break
		end
		timer += 1
		if timer >= 5 then
			--do stuff
			break --break out of loop when finished
		end
	end
end)

No, that’s not what I mean. Your code is doing something like:

  • Player stops moving, InputEnded fires
  • while loop begins
  • In the same time before 5 seconds, the player moves and InputBegan is fired, but the previous InputEnded loop hasn’t ended yet (and if InputEnded fired again you would be doubling the while loop execution?)

Added the fix before you replied, to explain, I took it from one of my own projects which didn’t require a check.

No it isn’t fixed yet

local players = game:GetService("Players")
local player = players.LocalPlayer or players.PlayerAdded:Wait()
local uis = game:GetService("UserInputService")
local timer = 0
local idle = false
local plrInteracting = false

uis.InputBegan:Connect(function()
    plrInteracting = true
	timer = 0
	idle = false
end)

uis.InputEnded:Connect(function()
    plrInteracting = false
    while not plrInteracting and task.wait(1) do
		timer += 1
		if timer >= 5 then
		    idle = true
			--do stuff
			break --break out of loop when finished
		end
	end
end)
local players = game:GetService("Players")
local player = players.LocalPlayer or players.PlayerAdded:Wait()
local uis = game:GetService("UserInputService")
local timer = 0
local idle = false

uis.InputBegan:Connect(function()
	timer = 0
	idle = false
end)

uis.InputEnded:Connect(function()
	idle = true
	while task.wait(1) do
		if not idle then
			break
		end
		timer += 1
		if timer >= 5 then
			--do stuff
			break --break out of loop when finished
		end
	end
end)

This is working fine my end.

Now I am not 100% sure if this will work but I really wanna do this in a render stepped. Do you know how could I wait a second using tick() then increase a value by 1?

task.wait(1)
--increment something by 1