Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why does this custom animation not stop when you move?

Asked by 5 years ago

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

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago
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)
Ad

Answer this question