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
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 |
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)