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

Playing a walk animation in a custom character? (Updated script)

Asked by
Chronomad 180
9 years ago

I have a script that plays an animation whenever W,A,S, or D are pressed . The animation ends in a loop, however I was wondering if there was a way I could get it to stop the animation when the key was released? Would it work with a looped animation? Do I need to remove the loop? I've tried adding an additional chunk of code using Service.InputEnded but the animation continued to play.

--EDIT-- Script now fires when key is released, And I've changed the animation to one that doesn't loop. It still doesn't work quite right however.

01        local Service = game:GetService("UserInputService")
02local animation = Instance.new("Animation")
03local player = game.Players.LocalPlayer
04local char = player.Character
05 
06Service.InputBegan:connect(function(input, recieved)
07    if input.UserInputType == Enum.UserInputType.Keyboard then
08        if input.KeyCode == Enum.KeyCode.W then
09 
10            repeat
11 
12            print('Input W')
13        animation.AnimationId = "http://www.roblox.com/Asset?ID=313344129"
14        local animTrack = player.Character.Humanoid:LoadAnimation(animation)
15            animTrack:Play()
View all 35 lines...

1 answer

Log in to vote
1
Answered by
Hero_ic 502 Moderation Voter
9 years ago

Its better to use the humanoid running function to make animations I also recommend making a function that plays the animations for you.

01--[[local animation = Instance.new("Animation") Our first change is that instead of doing this we do this]]
02local walkAnimation = Instance.new("Animation") --next we will already set the ID
03walkAnimation.AnimationId = "rbxassetid://313344129" --[[I recommend using rbxassetid:// it is quicker and saves time]]
04--Next we add a wait so we can have the player load
05wait(1)
06local player = game.Players.LocalPlayer
07local char = player.Character or player.Character:Added() --without this it could easily bug online
08local human = char:FindFirstChild("Humanoid") --allows us to get the humanoid to load animations
09local playingWalkAnimation = false --this is our debounce read down for more on it
10--Now we will make an function that plays animations for us!
11function playAnimation(animation)
12    --Next we make an animation tracker right here in this function
13    local aniTrack = human:LoadAnimation(animation)--we will call it this aniTrack and load the animation right now
14    aniTrack:Stop() --we stop the animation first then wait
15    wait()
View all 35 lines...

Hope this helped! ~KIHeros (There may be a few errors in this please tell me in the comments!)

0
that script wont stop the loop. DevingDev 346 — 8y
0
Id suggest to use the "Animate" script found inthe default player, all you will need to do is change the animation ids to your own Zendaya774isstupid 50 — 8y
Ad

Answer this question