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 7 years ago
Edited 7 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 — 7y

1 answer

Log in to vote
0
Answered by
Vid_eo 126
7 years ago
01local player = game.Players.LocalPlayer repeat wait() until player.Character.Humanoid
02local humanoid = player.Character.Humanoid
03local mouse = player:GetMouse()
04 
05local anim = Instance.new("Animation")
07 
08 
09mouse.KeyDown:connect(function(key)
10    if key == "e" then
11        while wait() do --loops animation
12            local playAnim = humanoid:LoadAnimation(anim)
13            playAnim:Play()
14            mouse.KeyUp:connect(function() --function happens when key isn't pressed
15                playAnim:Stop()
16        end)
17    end
18end)

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 — 7y
Ad

Answer this question