Afk Detector Not Working

Hello! I’m having trouble with an afk detector I found on the DevForum in a post from a few years ago.
This is the code:

local idle = false
local timer  = 0
local localplayer = game.Players.LocalPlayer
local getAway = script.Parent.Parent
getAway.ImageLabel.ImageTransparency = 1
local im = getAway.ImageLabel
getAway.Visible = false
local UIS = game:GetService("UserInputService")
UIS.InputEnded:Connect(function()
	idle = true
	while wait(1) do
		timer += 1
		if not idle then
			break
		end
		if timer >= 5 then
			getAway.Visible = true
			task.wait(0.1)
			getAway.Visible = false
			im.ImageTransparency = 0.9
			task.wait(1)
			getAway.Visible = true
			task.wait(0.1)
			getAway.Visible = false
			im.ImageTransparency = 0.6
			task.wait(1)
			getAway.Visible = true
			task.wait(0.1)
			getAway.Visible = false
			im.ImageTransparency = 0.4
			task.wait(1)
			print("afkplayer")
		end
	end
end)
UIS.InputBegan:Connect(function()
	idle = false
	timer = 0
end)

Of course I changed it a bit, but nothing is happening, and I don’t know why… Thanks for any support!
:slight_smile:

1 Like
local UIS = game:GetService('UserInputService')
local TimeSinceLastInput = 0

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

while true do
	TimeSinceLastInput += task.wait() -- task.wait() returns a number and still waits even if you use it as a number.

    if TimeSinceLastInput > 120 then
        -- do something
    end
end
2 Likes

Okay, I have this:

local UIS = game:GetService('UserInputService')
local TimeSinceLastInput = 0
local image = script.Parent
image.ImageTransparency = 1

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

while true do
	TimeSinceLastInput += task.wait() -- task.wait() returns a number and still waits even if you use it as a number.
	if TimeSinceLastInput > 10 then
		image.ImageTransparency = 0.8
		task.wait(0.25)
		image.ImageTransparency = 1
		task.wait(0.25)
		image.ImageTransparency = 0.8
		wait(0.25)
		image.ImageTransparency = 1
		task.wait(1)
		image.ImageTransparency = 0.5
		task.wait(0.25)
		image.ImageTransparency = 1
		task.wait(0.25)
		image.ImageTransparency = 0.5
		wait(0.25)
		image.ImageTransparency = 1
		task.wait(1)
		image.ImageTransparency = 0.2
		task.wait(0.25)
		image.ImageTransparency = 1
		task.wait(0.25)
		image.ImageTransparency = 0.2
		wait(0.25)
		image.ImageTransparency = 1
		task.wait(1)
	end
end

How do I make the image dissappear and stop appearing if the player moves again?

You could use a different method which is UserInputService.WindowFocused. It checks if the Roblox client is focused on the computer. UserInputService.WindowFocusReleased works oppositely, it checks if the window is unfocused.

1 Like

Thanks, but I’m trying to make a horror game where if you don’t move then text will come up on your screen saying MOVE until it jumpscares you, so that’s not rlly what I’m going for but thanks.

Well in that case, you might be looking for something like this.

https://devforum.roblox.com/t/detect-if-a-player-is-moving-or-not/1859067/4

1 Like

How can I reset this every time them move again?

	move = false
		task.wait(5)
		print("no")
		task.wait(0.2)
		image.ImageTransparency = 0.8
		task.wait(0.2)
		image.ImageTransparency = 1
		task.wait(0.2)
		image.ImageTransparency = 0.8
		task.wait(0.2)
		image.ImageTransparency = 1
		task.wait(1)
		image.ImageTransparency = 0.5
		task.wait(0.2)
		image.ImageTransparency = 1
		task.wait(0.2)
		image.ImageTransparency = 0.5
		task.wait(0.2)
		image.ImageTransparency = 1
		task.wait(1)
		image.ImageTransparency = 0.3
		task.wait(0.2)
		image.ImageTransparency = 1
		task.wait(0.2)
		image.ImageTransparency = 0.3
		task.wait(0.2)
		image.ImageTransparency = 1
		task.wait(1)
		image.ImageTransparency = 0.2
		task.wait(0.2)
		image.ImageTransparency = 1
		task.wait(0.2)
		image.ImageTransparency = 0.2
		task.wait(1)
		if move == false then
			Player:Kick("AFK")
		end
Full Script:
local Player = game.Players.LocalPlayer --local player
repeat wait(1) until Player.Character --repeats wait(1) until the character has loaded
local Character = Player.Character --character
local image = script.Parent
local move = false

local Humanoid = Character:FindFirstChild("Humanoid") --finds the humanoid from the players character

local RunService = game:GetService("RunService") --run service

local function IsPlayerMoving() --function
	if Humanoid.MoveDirection.Magnitude == 0 then --if the movedirection is equal to 0 then the following will happen;
		move = false
		task.wait(5)
		print("no")
		task.wait(0.2)
		image.ImageTransparency = 0.8
		task.wait(0.2)
		image.ImageTransparency = 1
		task.wait(0.2)
		image.ImageTransparency = 0.8
		task.wait(0.2)
		image.ImageTransparency = 1
		task.wait(1)
		image.ImageTransparency = 0.5
		task.wait(0.2)
		image.ImageTransparency = 1
		task.wait(0.2)
		image.ImageTransparency = 0.5
		task.wait(0.2)
		image.ImageTransparency = 1
		task.wait(1)
		image.ImageTransparency = 0.3
		task.wait(0.2)
		image.ImageTransparency = 1
		task.wait(0.2)
		image.ImageTransparency = 0.3
		task.wait(0.2)
		image.ImageTransparency = 1
		task.wait(1)
		image.ImageTransparency = 0.2
		task.wait(0.2)
		image.ImageTransparency = 1
		task.wait(0.2)
		image.ImageTransparency = 0.2
		task.wait(1)
		if move == false then
			Player:Kick("AFK")
		end
	elseif Humanoid.MoveDirection.Magnitude > 0 then --if the movedirection is greater than 0 then the following will happen;
		image.ImageTransparency = 1
		move = true
	end
end

RunService.RenderStepped:Connect(IsPlayerMoving) --running the function

It would probably be best if you put all of the waits and transparency changing inside a spawn(function()

You could check if the Magnitude is not the same as 0 in every wait().