Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I make an animation activate if player is AFK?

I'm trying to get my "idle" animation to only be activated if the player has not moved for 40 consecutive seconds. I've only managed to get the animation to play after 20 seconds if the player isn't moving by the 20th second, but this isn't really what I want:

-- This local script doesn't work correctly. It's meant to be an AFK animation, --
-- as in when the player has been idle without moving their mouse for 20 seconds, --
-- then the animation will play until the player moves their chacter --

local Player = game.Players.LocalPlayer
local Humanoid = Player.Character.Humanoid
local mouse = Player:GetMouse()
local animate = Instance.new("Animation")
animate.Name = "animate"
animate.AnimationId = "http://www.roblox.com/Asset?ID=2056648365"

debounce = false
	
mouse.Idle:connect(function(Idle)
if debounce == true then return end
debounce = true	

wait(9)

repeat wait() until Player.Character.HumanoidRootPart.Velocity == Vector3.new(0,0,0) == true do
print 'idle start'
		wait(1)
		if Player.Character.HumanoidRootPart.Velocity == Vector3.new(0,0,0) == false then return
			elseif Player.Character.HumanoidRootPart.Velocity == Vector3.new(0,0,0) == true then
		print 'Idle'
		script.Parent.Folder.IdleWallEvent:FireServer(Player) end
		local playAnimate = Humanoid:LoadAnimation(animate)
		playAnimate:Play()
	repeat wait() until Player.Character.HumanoidRootPart.Velocity == Vector3.new(1,1,1) == false do return end
		if Player.Character.HumanoidRootPart.Velocity == Vector3.new(1,1,1) then
		playAnimate:Stop()
		wait(0.01)
		print 'Idle Deactivated'
		end

		debounce = false
	end
end)

I then decided to use a script to insert my animation into the player, in hopes that I could make the animations play in an order. However, this was unsuccessful as well. I was trying to make it to where my custom animation wouldn't play unless the player had been idle for 20 seconds using weighting:

local Player = game.Players.LocalPlayer
local mouse = Player:GetMouse()
local Idle = Instance.new("Animation")
Idle.Name = "Idle"
Idle.AnimationId = "http://www.roblox.com/Asset?ID=2056648365"

Idle.Parent = Player.Character.Animate.idle

local Weight = Instance.new("NumberValue")
Weight.Name = "Weight"
Weight.Parent = Player.Character.Animate.idle.Idle
Weight.Value = 2

local OtherWeight = Player.Character.Animate.idle.Animation1.Weight
OtherWeight.Value = 4

local OtherOtherWeight = Player.Character.Animate.idle.Animation2.Weight
OtherOtherWeight.Value = 3

repeat wait(0.1) until Player.Character.LowerTorso.Orientation == Player.Character.HumanoidRootPart.Orientation + Vector3.new(10.38,0,0) do
print 'idle start'
		wait(1)
		if Player.Character.LowerTorso.Orientation == Player.Character.HumanoidRootPart.Orientation + Vector3.new(10.38,0,0) == false then return
			elseif Player.Character.LowerTorso.Orientation == Player.Character.HumanoidRootPart.Orientation + Vector3.new(10.38,0,0) == true then
		print 'Idle'
		wait(0.01)
		script.Parent.Folder.IdleWallEvent:FireServer(Player)
	end
end

And here's the function:

local Wall
local success, err = pcall(function()
  Wall = game:GetService("InsertService"):LoadAsset(2096107433)
end)
if success then
  Wall.Parent = workspace
  print("successfully inserted Wall")
else
  warn("unable to insert Wall because " .. err)
end
local Player = game.Players.LocalPlayer
local wall = game.Workspace.Model.SmoothBlockModel
 	wall.CanCollide = false
	wall.Anchored = true
	wall.Transparency = 0
	wait(0.1)
	wall.Orientation = Player.Character.HumanoidRootPart.Orientation
	wall.Position = Player.Character.LowerTorso.Position + Vector3.new(-0.051, 5.548, 2.177)
	wall.Transparency = 0
	
	local yes = Instance.new("Part")
	yes.Name = "yes"
	yes.Parent = game.Workspace
	yes.CanCollide = false
	yes.Anchored = true
	yes.Position = Player.Character.LowerTorso.Position
	yes.Transparency = 0
	
	repeat wait(0.5) until Player.Character.LowerTorso.CFrame == yes.CFrame == false do
			Wall:Destroy()
			print 'Wall Destroyed'
			yes:Destroy()
			print 'Almost Done'
		end
	end

script.Parent.Folder.IdleWallEvent.OnServerEvent:connect(wallcreate)

As you can see I am a little stumped and so I was hoping somebody on here could help me.

1 answer

https://wiki.roblox.com/index.php?title=API:Class/Player/Idled

Player.Idled:Connect(function(idledTime)
	code
end)

Edit code: Note: Sometimes it won't work, so I added some stuff.

wait()
local plr = game:GetService("Players").LocalPlayer
local c = plr.Character or plr.CharacterAdded:Wait()
repeat
	wait()
until c ~= nil
local torso = c:WaitForChild("Torso")
local oldPos, idled = nil, false
torso.Changed:Connect(function(diff)
	if diff == "Position then
		if torso.Position == oldPos then
			idled = true
		end
	end
end)
spawn(function()
	while idled do
		code
		wait(1)
	end
end)
while wait(1) do
	oldPos = torso.Position
end

24 comments

0

how do I change the amount of time needed? What do I do with this code? I'm finding it hard to understand what you mean, could you be a little more descriptive?

0

Just put in the parenthesis where "idledTime" is the amount of time you want before the code to run.

0

^

0

One last question, how do I differentiate between 40 seconds and 40 minutes?

0

I'm not sure what you mean by that, but 40 minutes is 40 * 60.

0

I guess my question was needlessly complicated, all I meant was how do I make the time 40 seconds?

0

idledTime is how long the player idled.

0

right, but my problem is that I need the script to only run if the player has been idle for 40 seconds, how do I make THAT happen

0

Just do: if idledTime == 40 then

0

I tried this and now all of a sudden the entire script no longer works

0

Wiki page says: Description: Usually fired two minutes after the game engine classes the player as idle. Time is the amount of seconds since this point.

0

So I need to wait 2 minutes and 40 seconds for this to work or...

0

Yeah

0

I stood still for well over 3 minutes and nothing happened

0

Oh you could use the changed event for a limb for the position.

0

Hm... could you elaborate?

0

You could detect if the current character's position is the same as the last character's position. If so, there idled.

0

I'm not sure I understand, could you write out an example line of code?

0

The code may not work.

0

oh, why is that?

0

The player is always moving because of the Roblox (which rotates the limbs sometimes) animations itself, so I recommend making a invisible part that cannot be rotated.

0

Also you could look in the Roblox "Animate" script in your character in studio and see how they do it.

0

I'll most likely go with an invisible, non rotational part. So I make a line of code that detects if the part stays in the same place for a certain amount of time, correct? And then this introduces MY animation, correct?

0

Yes, you can also look in the Animate script and see how Roblox did it.

This discussion is preserved as it was. New answers, comments, and votes are closed.