Custom animation script issues

I’m making a custom animation script (with accurate footsteps) and it has some issues that i don’t how how to solve.

  1. What do you want to achieve? I want to create a simple animation script with accurate footstep sounds to give me easier access to changing animations (Hope that makes sense.)

  2. What is the issue? The animations don’t play, nor does the default animation script delete itself like I want it to.

  3. What solutions have you tried so far? I looked for solutions on more topics, and took many glances and changed my script, but nothing I tried worked.

Here’s the script (sorry if it’s messy):


local plr = game:GetService("Players").LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
local root = char:WaitForChild("HumanoidRootPart")

local step = script.Sound

local runanim = script.run
local walkanim = script.walk
local jumpanim = script.jump
local fallanim = script.fall

local run = hum:LoadAnimation(runanim)
local walk = hum:LoadAnimation(walkanim)
local jump = hum:LoadAnimation(jumpanim)
local fall = hum:LoadAnimation(fallanim)

while true do
	char:WaitForChild("Animate"):Destroy()
	if hum.WalkSpeed <= 14 then
		if hum:GetState() == Enum.HumanoidStateType.Running then
			walk:Play()
			walk:GetMarkerReachedSignal("Left"):Connect(foot())
			walk:GetMarkerReachedSignal("Right"):Connect(foot())
		else
			walk:Stop()
		end
	elseif hum.WalkSpeed >= 14 then
		if hum:GetState() == Enum.HumanoidStateType.Running then
			run:Play()
			run:GetMarkerReachedSignal("Left"):Connect(foot())
			run:GetMarkerReachedSignal("Right"):Connect(foot())
		else
			run:Stop()
		end
	end
end

function foot()
	step:Play()
end

If you have any info I could use, or pieces of a script then please tell me

Note: the game is in R6, and I do not know much about scripting, so please use detail.

Edit: I forgot to mention that the script is a LocalScript in StarterCharacterScripts

It looks like your code is an infinite loop, which will instantly crash studio. It is a much better idea to use RunService.RenderStepped() instead. It also looks like the foot() function you made isnt being used in the code snippet. You can make accurate footsteps by making animation events on the frame where the player’s foot touches the ground. You can then check if you’ve reached the animation marker/event by using Track:GetMarkerReachedSignal(“MarkerName”)

1 Like

thanks! i’ll try this right now!

I had just realized that this part is in the loop, which will throw an error since the default Animate script no longer exists. You should put this line outside of the loop.

1 Like

Thank you, but now the animations and the step sounds just play constantly in a loop, do you have any idea on how i could fix that?

What is the script you are currently using right now?

1 Like
local runserv = game:GetService("RunService")

local plr = game:GetService("Players").LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
char:WaitForChild("Animate"):Destroy()
local hum = char:WaitForChild("Humanoid")
local root = char:WaitForChild("HumanoidRootPart")

local step = script.Sound

local runanim = script.run
local walkanim = script.walk
local jumpanim = script.jump
local fallanim = script.fall

local run = hum:LoadAnimation(runanim)
local walk = hum:LoadAnimation(walkanim)
local jump = hum:LoadAnimation(jumpanim)
local fall = hum:LoadAnimation(fallanim)

function foot()
	step:Play()
end

runserv.RenderStepped:Connect(function()
	if hum.WalkSpeed <= 14 then
		if hum:GetState() == Enum.HumanoidStateType.Running then
			walk:Play()
			walk:GetMarkerReachedSignal("Left"):Connect(foot())
			walk:GetMarkerReachedSignal("Right"):Connect(foot())
		else hum:GetState() == Enum.HumanoidStateType.
			walk:Stop()
		end
	elseif hum.WalkSpeed >= 14 then
		if hum:GetState() == Enum.HumanoidStateType.Running then
			run:Play()
			run:GetMarkerReachedSignal("Left"):Connect(foot())
			run:GetMarkerReachedSignal("Right"):Connect(foot())
		else
			run:Stop()
		end
	end
end)

Is the footstep sound you are using looped?

1 Like

im pretty sure it isnt?
image

Try using destroying the sound after it is played using Sound.Ended(). Also make sure to clone the sound and play the sound clone instead.

function foot()
    local Sound = step:Clone()
    Sound.Parent = root
    Sound:Play()
    task.spawn(function()
		Sound.Ended:Wait()
		Sound:Destroy()
	end)
end
1 Like

its still looping the animation and sound? i don’t understand what’s wrong

local runserv = game:GetService("RunService")

local plr = game:GetService("Players").LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
char:WaitForChild("Animate"):Destroy()
local hum = char:WaitForChild("Humanoid")
local root = char:WaitForChild("HumanoidRootPart")

local step = script.Sound

local runanim = script.run
local walkanim = script.walk
local jumpanim = script.jump
local fallanim = script.fall

local run = hum:LoadAnimation(runanim)
local walk = hum:LoadAnimation(walkanim)
local jump = hum:LoadAnimation(jumpanim)
local fall = hum:LoadAnimation(fallanim)

function foot()
	local Sound = step:Clone()
	Sound.Parent = root
	Sound:Play()
	task.spawn(function()
		Sound.Ended:Wait()
		Sound:Destroy()
	end)
end

runserv.RenderStepped:Connect(function()
	if hum.WalkSpeed <= 14 then
		if hum:GetState() == Enum.HumanoidStateType.Running then
			walk:Play()
			walk:GetMarkerReachedSignal("Left"):Connect(foot())
			walk:GetMarkerReachedSignal("Right"):Connect(foot())
		else
			walk:Stop()
		end
	elseif hum.WalkSpeed >= 14 then
		if hum:GetState() == Enum.HumanoidStateType.Running then
			run:Play()
			run:GetMarkerReachedSignal("Left"):Connect(foot())
			run:GetMarkerReachedSignal("Right"):Connect(foot())
		else
			run:Stop()
		end
	end
end)
function foot()
	local Sound = Instance.new("Sound")
	Sound.Parent = root
    Sound.SoundId = "rbxassetid://" -- Your id here
    Sound.Volume = 0.5
   -- Do keep in mind in order for the sound to lose volume the further away it is it needs to be parented to a part
    Sound.RollOffMinDistance = 10 -- This is the minimum amount of distance you need to be from the sound's origin for it to start to lose volume
    Sound.RollOffMaxDistance = 50 -- This is the maximum distance you can hear the sound before you cant hear it
	Sound:Play()
	task.spawn(function()
		Sound.Ended:Wait()
		Sound:Destroy()
	end)
end

Does this work?

1 Like

Ah, I see the problem now

You are playing the animation and restarting it every frame. You can avoid this by checking if the animation is not playing using Animation.IsPlaying in an if statement.

runserv.RenderStepped:Connect(function()
	if hum.WalkSpeed <= 14 then
		if hum:GetState() == Enum.HumanoidStateType.Running and not walk.IsPlaying then
			if run.IsPlaying then run:Stop() end -- Stops the running animation
            walk:Play()
			walk:GetMarkerReachedSignal("Left"):Connect(foot())
			walk:GetMarkerReachedSignal("Right"):Connect(foot())
		end
	elseif hum.WalkSpeed >= 14 then
		if hum:GetState() == Enum.HumanoidStateType.Running and not run.IsPlaying then
			if walk.IsPlaying then walk:Stop() end -- Stops the walking animation
            run:Play()
			run:GetMarkerReachedSignal("Left"):Connect(foot())
			run:GetMarkerReachedSignal("Right"):Connect(foot())
		end
	end
end)

thanks!, i’ll try it and see if it works this time.

I edited the post, use the new version instead.

it fixes the sound and animation playing over and over, but the sound doesnt play with the animation events, only plays when the animation starts, and the animation doesnt stop when i stop moving. any idea how to fix this? sorry about asking so many questions, lol

runserv.RenderStepped:Connect(function()
	if hum.WalkSpeed <= 14 and hum.MoveDirection.Magnitude > 0 then
		if hum:GetState() == Enum.HumanoidStateType.Running and not walk.IsPlaying then
			if run.IsPlaying then run:Stop() end -- Stops the running animation
            walk:Play()
			walk:GetMarkerReachedSignal("Left"):Connect(foot())
			walk:GetMarkerReachedSignal("Right"):Connect(foot())
		end
	elseif hum.WalkSpeed >= 14 and hum.MoveDirection.Magnitude > 0 then
		if hum:GetState() == Enum.HumanoidStateType.Running and not run.IsPlaying then
			if walk.IsPlaying then walk:Stop() end -- Stops the walking animation
            run:Play()
			run:GetMarkerReachedSignal("Left"):Connect(foot())
			run:GetMarkerReachedSignal("Right"):Connect(foot())
		end
	else
        if run.IsPlaying then run:Stop() end
        if walk.IsPlaying then walk:Stop() end
    end
end)

Humanoid.MoveDirection.Magnitude will be greater than 0 when the player is moving, and Humanoid.MoveDirection.Magnitude will be 0 when the player is not moving

1 Like

thanks, but the sound still doesnt play when the animation event passes,

Are the Animation Events in the animation itself?

Yeah, they are in the animation