Footstep sound doesn't stop when player stop moving

I’m trying to make a custom footstep script I already completed making another script to disable default one but now I’m trying to make a custom footstep script but when the character stops moving with the footstep sound playing it doesn’t stop

local Player = game.Players.LocalPlayer
task.wait(4)
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")
local FootStepSound = script:WaitForChild("GMod Tile Footsteps")
local Cooldown = false
local Soundisplaying = false

while true do
	task.wait(0.01)
		print("yes1")
		if Humanoid.MoveDirection.Magnitude > 0 and Soundisplaying == false then
		FootStepSound:Play()
		Soundisplaying = true
		if Humanoid.MoveDirection.Magnitude == 0 then
			Soundisplaying = false
		end
		if Soundisplaying == true then
		task.wait(FootStepSound.TimeLength)
		end	
		elseif Humanoid:GetState() == Enum.HumanoidStateType.None or Cooldown == true then
		FootStepSound:Stop()
		end
end

Avoid using while true loops where possible, as they can be messy and detrimental to performance in your game. Instead, always check for events you can use. In this example, you should use the Humanoid.Running event to get the speed it returns and use that to play and stop the footstep sounds.

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local FootStepSound = script:WaitForChild("GMod Tile Footsteps")
local Cooldown = false
local SoundIsPlaying = false

local function playFootstepSound()
    if not Cooldown and Humanoid.MoveDirection.Magnitude > 0 then
        FootStepSound:Play()
        SoundIsPlaying = true
        Cooldown = true
        task.wait(FootStepSound.TimeLength)
        Cooldown = false
        SoundIsPlaying = false
    end
end

local function HumanoidRunning(speed)
    if speed > 0 and not SoundIsPlaying then
        playFootstepSound()
    elseif speed == 0 and SoundIsPlaying then
        FootStepSound:Stop()
        SoundIsPlaying = false
        Cooldown = false
    end
end

Humanoid.Running:Connect(HumanoidRunning)
1 Like

IT WORKS THANK YOU SO MUCH!!! I will try my best not to use while true loops

1 Like

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