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

How do i loop an animation when "E" is pressed?

Asked by 6 years ago
Edited 6 years ago

I'm trying to make a crawl animation play when you click "E" but i want it to loop until i release "E"... How?

Code:

local player = game.Players.LocalPlayer repeat wait() until player.Character.Humanoid local humanoid = player.Character.Humanoid local mouse = player:GetMouse()

local anim = Instance.new("Animation") anim.AnimationId = "http://www.roblox.com/asset/?id=1267282598"

mouse.KeyDown:connect(function(key) if key == "e" then local playAnim = humanoid:LoadAnimation(anim) playAnim:Play() end end)

The animation is 0.1 seconds long so do i just have to make it like 100 sec long and it will automatically loop until i no longer press it?

0
You could create a loop when e is pressed. StoleYourClothes 105 — 6y

1 answer

Log in to vote
0
Answered by
Vid_eo 126
6 years ago
local player = game.Players.LocalPlayer repeat wait() until player.Character.Humanoid 
local humanoid = player.Character.Humanoid 
local mouse = player:GetMouse()

local anim = Instance.new("Animation") 
anim.AnimationId = "http://www.roblox.com/asset/?id=1267282598"


mouse.KeyDown:connect(function(key) 
    if key == "e" then
        while wait() do --loops animation
            local playAnim = humanoid:LoadAnimation(anim) 
            playAnim:Play() 
            mouse.KeyUp:connect(function() --function happens when key isn't pressed
                playAnim:Stop() 
        end)
    end 
end)

All I did was add a while loop in order to loop the animation. Inside the loop, I played the animation & put in a function that stops the animation. The function occurs when the key stops being pressed.

I think you could also put in while wait() and key == "e" do instead of the function, but I'm not sure.

If this answered your question, please mark this as the answer. If not, tell me why in the comments. Thanks!

0
keyDown is deprecated, use UserInputService instead oSyM8V3N 429 — 6y
Ad

Answer this question