Footstep Spamming Sounds

im trying to script footsteps but it spams the sound and lags alot
here is the server script

local hum = script.Parent:FindFirstChildWhichIsA(“Humanoid”)

local footstepfolder = game.ReplicatedStorage.Footsteps
local debounce = false

script.Parent.RemoteEvents.Footstep.OnServerEvent:Connect(function(player,floor)
if floor.Name ~= “Air” then

  hum.Running:Connect(function(speed)	

  	if speed ~= 0 then
  		local function footstep()
  			if debounce == true then	
  				local selected = footstepfolder[floor.Name]:GetChildren()
  				local cloned = selected[math.random(1,#selected)]:Clone()
  				local destroy = script.DestroySound:Clone()
  				cloned.Parent = script.Parent.Torso
  				destroy.Parent = cloned
  				destroy.Enabled = true
  				cloned:Play()
  			end
  		end
  		if hum.WalkSpeed == 16 then
  			debounce = false
  			task.wait(0.5)
  			debounce = true
  			footstep()
  		elseif hum.WalkSpeed >= 16 then
  			debounce = false
  			task.wait(0.2)
  			debounce = true
  			footstep()
  		elseif hum.WalkSpeed <= 16 then
  			debounce = false
  			task.wait(1.2)
  			debounce = true
  			footstep()
  		end		
  	end
  end)	

end
end)
and heres the local script

local hum = script.Parent:FindFirstChildWhichIsA(“Humanoid”)

local footstepfolder = game.ReplicatedStorage.Footsteps

hum.Running:Connect(function()
wait(1)
local floor = hum.FloorMaterial
script.Parent.RemoteEvents.Footstep:FireServer(floor)

end)

7 Likes
External Media

the Clip

2 Likes

Your sounds which is playing if humanoid is running are spamable because the code is not delayed. Delay the footstep function in server script code by any value using wait() or task.wait().

2 Likes

i added a task wait but it the sound still spams but now it doesnt lag as it was before but still lags of the sounds

2 Likes

Could you show me your edited code in server script?

2 Likes

2 Likes

It’s not what i have really expected but you edited it wrong. As i said, you should’ve edit the footstep function and delay it.

 local function footstep()
  			if debounce == true then	
  				local selected = footstepfolder[floor.Name]:GetChildren()
  				local cloned = selected[math.random(1,#selected)]:Clone()
  				local destroy = script.DestroySound:Clone()
  				cloned.Parent = script.Parent.Torso
  				destroy.Parent = cloned
  				destroy.Enabled = true
                                task.wait(0.95) -- here you can edit value
  				cloned:Play()
  		end
end
2 Likes

Oh Sorry it works ok but the still the sound plays multiple at times

2 Likes

also kinda still lags i dont know why, im new to scripting

1 Like

Alright then try adding debounce to your local script code. It maybe happens because code executes multiple times when player makes steps. It should be my final solution now

3 Likes

I meant local script, not server.
The function that he has given is only provided in server script. But debounce is already exist in the server so there’s should be any point of adding debounce to local script code.

2 Likes

So true I am an such an idiot. lol

1 Like

like this?

local hum = script.Parent:FindFirstChildWhichIsA(“Humanoid”)

local footstepfolder = game.ReplicatedStorage.Footsteps
local debounce = true

hum.Running:Connect(function()
if debounce == true then
wait(0.3)
local floor = hum.FloorMaterial
script.Parent.RemoteEvents.Footstep:FireServer(floor)
debounce = false
else
wait(0.3)
debounce = true
end
end)

2 Likes

i slammed my face to write this:

-------------------------------------Paste into localscript------------------------------------
local player=game.Players.LocalPlayer
local char=player.Character
local hum=char:FindFirstChildOfClass('Humanoid')
local running=false
local soundtable={'4925090422','4925090422'}--Paste your soundids
local timer=tick()
hum.Running:Connect(function(sp)
	if sp > 0 then
		running=true
	else
		running=false
	end
end)
game:GetService('RunService').Heartbeat:Connect(function(renderdelta)
	if tick()-timer+renderdelta >= 6/hum.WalkSpeed and running then -- adjust this to your liking
		timer=tick()
		local soundinstance=Instance.new('Sound',char:FindFirstChild('HumanoidRootPart'))
		soundinstance.SoundId='rbxassetid://'..soundtable[math.random(1,2)]--also adjustable
		soundinstance:Play()
		game:GetService("Debris"):AddItem(soundinstance,5)
	end
end)

you can add more debounce or use my script that i typed, to replicate it on server just use remotes. (Also tip for pro gamers: this might lag alot so edit this out on your own! :smiling_face:)

4 Likes

Thank you Very Much!
i Modified Your Script So Everytime you step its sends a remote event
that play a footstep on a server script and it works well
also i never knew how debris worked until you made this script
thank you very much

1 Like

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