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.

24 comments
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?
Just put in the parenthesis where "idledTime" is the amount of time you want before the code to run.
^
One last question, how do I differentiate between 40 seconds and 40 minutes?
I'm not sure what you mean by that, but 40 minutes is 40 * 60.
I guess my question was needlessly complicated, all I meant was how do I make the time 40 seconds?
idledTime is how long the player idled.
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
Just do: if idledTime == 40 then
I tried this and now all of a sudden the entire script no longer works
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.
So I need to wait 2 minutes and 40 seconds for this to work or...
Yeah
I stood still for well over 3 minutes and nothing happened
Oh you could use the changed event for a limb for the position.
Hm... could you elaborate?
You could detect if the current character's position is the same as the last character's position. If so, there idled.
I'm not sure I understand, could you write out an example line of code?
The code may not work.
oh, why is that?
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.
Also you could look in the Roblox "Animate" script in your character in studio and see how they do it.
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?
Yes, you can also look in the Animate script and see how Roblox did it.