Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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.

		local Service = game:GetService("UserInputService")
local animation = Instance.new("Animation")
local player = game.Players.LocalPlayer
local char = player.Character

Service.InputBegan:connect(function(input, recieved)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.W then
			
			repeat
			
			print('Input W')
		animation.AnimationId = "http://www.roblox.com/Asset?ID=313344129" 
		local animTrack = player.Character.Humanoid:LoadAnimation(animation) 
			animTrack:Play()
			wait(1)
			
			
		
		until
			
			Service.InputEnded:connect(function(input, recieved)
			if input.UserInputType == Enum.UserInputType.Keyboard then
			if input.KeyCode == Enum.KeyCode.W then
			
			print('Input W stahped')
		local animTrack = player.Character.Humanoid:LoadAnimation(animation) 
			animTrack:Stop()
			
					end
				end
			end)
		end
	end
end)

1 answer

✓ Accepted answer

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

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

--Next we make an on running function
function onRunning(speed) --speed gets the player speed
	if speed > 1 then --[[we check if the speed is greater then 1 and not 0 or the animation will play when you land]]
		--next I suggest adding a debounce so the animation does not jitter
		if playingWalkAnimation == false then -- this will be our debounce
		playingWalkAnimation = true --[[setting it to true will stop the animation from looping over itself]]
		playAnimation(walkAnimation) --now it shall play our animation using this function!
		end
	else
		--Next we set the debounce to false so we can play the animmation when running again
		playingWalkAnimation = false
		--we can add the idle animation here if you like
	end
end
--And finally we add a connect function so the onRunning function will even work
human.Running:connect(OnRunning)
--And that is it!

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

2 comments

0

that script wont stop the loop.

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

This discussion is preserved as it was. New answers, comments, and votes are closed.