Trying to Make a Simple AFK Effect

Hi Everyone, I’m trying to make a simple AFK effect, so far I made a simple Lobby and I want the players to have a simple effect if they didn’t move for a while

image

what I’m trying to achieve is, if the player didn’t move for like 5 mins, sleeping particles start to appear from his head, and once the player move again the particles disappear

I’m not really good when it’s about scripting, hopefully you can help me and thank you :slight_smile:

3 Likes
local Minutes = 5 -- how many minutes before the player is marked as AFK
local threshold = (60 * Minutes)

local localPlayer = game.Players.LocalPlayer

local hum = script.Parent:WaitForChild('Humanoid')
local remote = game.ReplicatedStorage:WaitForChild('RemoteEvent')

local afk

while true do
	if not hum.Parent then
		break
	end
	if hum.MoveDirection.magnitude > .01 then
		-- the player is moving. let the server know
		stoppedMoving = nil
		if afk then
			afk = nil
			remote:FireServer()
		end
	elseif not stoppedMoving then
		-- the player has stopped moving, mark it
		-- imagine os.clock as a number that increases over time. good for benchmarking and keeping time
		stoppedMoving = os.clock()
	end
	-- if the player has stopped moving, check how long
	if stoppedMoving and os.clock() - stoppedMoving > threshold and not afk then
		afk = true
		remote:FireServer(true)
	end
	wait(1)
end

Put it in a Localscript in StarterCharacterScripts.

Make a RemoteEvent in ReplicatedStorage.

Then, make a server script and put it in ServerScriptService:

local remote = game.ReplicatedStorage:WaitForChild('RemoteEvent')
local particlePrototype = game.ReplicatedStorage.particles -- replace this

remote.OnServerEvent:connect(function(plyr, AFK)
	if plyr.Character then
		local head = plyr.Character:FindFirstChild('Head')
		if head then
			local particles = head:FindFirstChild(particlePrototype.Name)
			if AFK and not particles then
				local p = particlePrototype:Clone()
				p.Parent = head
			elseif not AFK and particles then
				particles:Destroy()
			end
		end
	end
end)

I havent tested this. Let me know if there are any problems and I’ll help

3 Likes

it’s working very well, thank you <3

also if you don’t mind, is there’s a way to make the particle enable set to false before the destroy?
cause the particles suddenly disappear instead of vanishing slowly when the player move

local remote = game.ReplicatedStorage:WaitForChild('RemoteEvent')
local particlePrototype = game.ReplicatedStorage.particles -- replace this

remote.OnServerEvent:connect(function(plyr, AFK)
	if plyr.Character then
		local head = plyr.Character:FindFirstChild('Head')
		if head then
			local particles = head:FindFirstChild(particlePrototype.Name)
			if not particles then
				particles = particlePrototype:Clone()
				particles.Parent = head
			end
			particles.Enabled = (AFK == true)
		end
	end
end)
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.