How to check if a player has / has not done something for a period of time

  1. What do you want to achieve? Keep it simple and clear!

Hi, I go by Restored or Carisard, I would like to know how I would go about creating a script that checks if a player for example has / has not walked in the last 3 seconds. If this is anything you can help with, then I would really appreciate your help.

  1. What is the issue? Include screenshots / videos if possible!

I guess that I can not figure it out?

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I found a post on devforum with a similar problem but still not similar enough so I tried changing the code up a bit but still couldn’t get it to work, can not find anything on the Developer Hub either.

You should use tick() for this.

1 Like

Usages of os.time() which acts as a timer and RunService.Heartbeat event acts as a repetitive loop to check the difference between the initial time and the current time can help you.

If I remember correctly, tick() is about to be deprecated soon as mentioned by an experienced scripter.

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

There’s a specific event for this which fires roughly two minutes after a player’s activity stops.

1 Like

I need it to be 3 seconds. 2 minutes is way too long unfortunately. I am looking into os.time though.

local run = game:GetService("RunService")
local userInput = game:GetService("UserInputService")

local lastActivity = tick()

run.RenderStepped:Connect(function()
	if tick() - lastActivity > 3 then
		print("Player is inactive!")
	end
end)

userInput.InputChanged:Connect(function(input)
	lastActivity = tick()
end)

You can listen for user input to determine if a user hasn’t performed an action for 3 or more seconds.

3 Likes

thank you so much, ima try to play around with this for a while and try to understand it a little more, once again, thank you

I think you should use the InputBegan event because if you use InputChanged event, it will fire only when input type changed, so if player is just moving their mouse and nothing else, it will still count as no activity.