Footstep audio not working after a couple steps

Footstep audio not working after a couple of steps

So I have this script that basically handles my custom footstep audio but after a few steps it stops working? I have to stop walking and then start again for it to work again but then it stops again after a bit. This seems simple but I can’t find out why this code isn’t working correctly. Is there any reason why this is happening? :sob:

Code (Local script in StarterCharacterScripts):

local player = game.Players.LocalPlayer
local char = player.Character
local hum = char.Humanoid

local footStepId = game.Workspace.Sfx.Footstep

local walkDelay = false

hum.Running:Connect(function(spd) -- enables footsteps
	if spd > 0 and walkDelay == false then
		walkDelay = true
		footStepId:Play()
		task.wait(footStepId.TimeLength * 1.6)
		walkDelay = false
	end
end)

Exactly how many footsteps can you hear before it stops?

Try replacing ‘spd > 0’ with ‘hum.MoveDirection.Magnitude > 0’
If that doesn’t work, try something like ‘print(WalkDelay)’ to see if it stays false while running

local player = game.Players.LocalPlayer
local char = player.Character
local hum = char.Humanoid

local walkDelay = false

hum.Running:Connect(function(spd) -- enables footsteps
print(walkDelay)
	if hum.MoveDirection.Magnitude > 0 and not walkDelay then
		walkDelay = true
local footStepId = game.Workspace.Sfx:WaitForChild("Footstep"):Clone()
footStepId.Parent = workspace
		footStepId:Play()
game:GetService("Debris"):AddItem(footStepId,3)
		task.wait(footStepId.TimeLength * 1.6)
		walkDelay = false
	end
end)

Tweaked a few things that might break the code

So, one issue to your problem is that .Running only fires when the players move direction changes from what it is. So things like starting to run fire it, changing direction of running while running, jumping all fire it. One way I would do it is using the magnitude of the movedirection of the humanoid but of course things happen where the player can be falling and it would make footsteps because falling would change the movedirection magnitude. To solve this you can raycast a certain distance and if there is an instance then set a walking value to true, and then play the sound only if that value is true, else set it to false.

Here is a video that demonstrates the .Running problem. Look in the output and you can see that it only fires when I change direction or start moving.

The video quality is a little bad sorry

Okay here took your script added raycasting to check if the player is grounded and then checked the move direction magnitude to see whether or not to play the sound and then ended up with this:

local player = game.Players.LocalPlayer
local char = player.Character
local hum = char.Humanoid

local footStepId = game.Workspace.Sfx.Footstep

local walkDelay = false

local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Exclude 
params.FilterDescendantsInstances = {char} 
params.RespectCanCollide = true
while task.wait(footStepId.TimeLength / 4) do -- change how fast here
	direction = -(char.PrimaryPart.CFrame.UpVector) * 5 --adjust distance here
	ray = workspace:Raycast(char.PrimaryPart.Position, direction, params)
	if ray and ray.Instance then
		print(ray.Instance)
		if ray.Instance then
			print("Player is grounded")
			isgrounded = true
		else
			isgrounded = false
		end
		if isgrounded == true and hum.MoveDirection.Magnitude > 0 then
			footStepId:Play()
		end
	else
		if ray and not ray.Instance then
			warn("The instance does not exist")
		else if not ray then
				warn("The ray does not exist")
			end
		end
	end
end

You can adjust the timing or the amount in studs how far the raycast goes to be more precise but here it is.

Thanks for this! Also can you explain a little what Humanoid.MoveDirection.Magnitude does?

Humanoid.MoveDirection.Magnitude returns a value which is the movement of the player, so if the player was moving forward then it might give a value like 1 if the player is moving at a constant speed. The movedirection is the direction the player is moving in, simple enough. And the magnitude just returns that somewhere between a 1 and a 0 I think. I use it for stuff like sprint systems. It is a singular value, not a Vector3 like MoveDirection.

Edit:
Also, if I were you, I would remove the warning things in the raycasting area because it spams the output and on the analytics page, it will show all those warnings.
So just remove these lines:

warn("The instance does not exist")
--and this one
warn("The ray does not exist")
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.