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

Why does this custom animation not stop when you move?

I'm trying to make a sleeping animation but you stay sleeping even when you move.

prefix = "/e "
game.Players.PlayerAdded:connect(function(p)
p.Chatted:connect(function(m)
m = m:lower()
if m == prefix.."sleep" then
local a = Instance.new("Animation",p.Character)
a.AnimationId = "rbxassetid://2053583627"
local h = p.Character:FindFirstChild'Humanoid'
local t = h:LoadAnimation(a)
t:Play()
workspace.Player.Humanoid.Running:connect(function(speed)
	 if speed > 0 then
		t:Stop()
	end
end)
end
end)
end)

Thanks if you can help

1 answer

Problem Solution
connect is deprecated use Connect
The parent parameter for Instance.new is deprecated Assign the parent of the object in another line
You’re adding an animation every time the player chats the command and could lag because of too many objects Create an if statement that checks if the animation is in them

###You’re setting up an event that checks if it’s walking to stop the animation, this is just too much work. Simply set the walk speed to 0.

game:GetService("Players").PlayerAdded:Connect(function(plr)
    plr.Chatted:Connect(function(msg, recipient)
        if msg == "/e sleep" and not recipient then
            if not plr.Character:FindFirstChild"SleepAnim" then
                local animation = Instance.new"Animation"
                animation.AnimationId = "rbxassetid://2053583627"
                animation.Name = "SleepAnim"
                animation.Parent = plr.Character
            
                local animTrack = plr.Character.Humanoid:LoadAnimation(animation)
                animTrack:Play()
            
                while animTrack.IsPlaying do
                    plr.Character.Humanoid.WalkSpeed = 0 
                    wait()
                end
            end
        end

    end)
end)

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