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)
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().
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
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
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.
-------------------------------------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! )
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