I have this code for a sitting animation, I have a toggle script with it. Its looped in of its own (used an animation Editor) and it works that way. When I press Z, the character sits forever, when I press Z again it's supposed to stand up and keep walking, yet it doesn't!
local player = game.Players.LocalPlayer repeat wait() until player.Character.Humanoid local humanoid = player.Character.Humanoid local mouse = player:GetMouse() On = false --ANIMATION-- local pushupanim = Instance.new("Animation") pushupanim.AnimationId = "http://www.roblox.com/asset/?id=3408464847" mouse.KeyDown:connect(function(key) if key == "z" and On == false then local playAnim = humanoid:LoadAnimation(pushupanim) playAnim.Priority = Enum.AnimationPriority.Action playAnim:Play() humanoid.WalkSpeed = 0 On = true elseif key == "z" and On == true then local playAnim = humanoid:LoadAnimation(pushupanim) playAnim.Priority = Enum.AnimationPriority.Action playAnim:Stop() humanoid.WalkSpeed = 16 On = false end end)
After the "elseif", the Humanoid.WalkSpeed = 16 Does work, yet the animation doesn't stop.
The problem here is that both of the playAnim
s you use are different animations you are loading. This is because these are not global variables and can only be accessed in their respective if
statements. The simple fix to this is to just load the animation BEFORE the if statements. If you can't, make a new variable equal to nil
, and set it to your loaded animation if it's nil and do nothing if it's not.