Hello, today I tried to make a footstep script on my own, I succeeded. But then when I ran, I noticed that every time I press the shift key, the sounds overlap. How can I fix this?
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Debris = game:GetService("Debris")
local MaterialTable = {
["Concrete"] = {Sounds = game.SoundService.FootSteps.Concrete},
["Fabric"] = {Sounds = game.SoundService.FootSteps.Fabric},
["Grass"] = {Sounds = game.SoundService.FootSteps.Grass}
}
local HRP = Character:WaitForChild("HumanoidRootPart")
local function castRay()
local origin = HRP.Position
local direction = Vector3.new(0, -10, 0)
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {Character}
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
-- Create Ray And Get The Results
local result = workspace:Raycast(origin, direction, raycastParams)
if result then
local hitPart = result.Instance
local material = result.Material
return material.Name
else
print("Ray bir Ĺźeyle temas etmedi.")
end
end
Character.Humanoid.AnimationPlayed:Connect(function(track)
if track.Name == "RunAnim" or track.Name == "WalkAnim" then
track:GetMarkerReachedSignal("Footstep"):Connect(function()
local Material = castRay()
print(Material)
local Sounds = MaterialTable[Material].Sounds:GetDescendants()
local RandomNumber = math.random(1, #Sounds)
local RandomSound = Sounds[RandomNumber]
local ClonedSound = RandomSound:Clone()
ClonedSound.Parent = Character
ClonedSound:Play()
ClonedSound.Ended:Connect(function()
ClonedSound:Destroy()
end)
end)
end
end)
What do you mean is it the same sound or 2 completely different sounds?
Because in the second case the overlapping sound could probably be the running sound inside of the HumanoidRootPart
In that case destroy the Running sound inside of the HumanoidRootPart
Edit: Apologies, I didn’t see the video link in that case this could be due to the walk animation or Run Animations speed being way to high so the sounds overlap so the marker is reached way to quickly. (Sorry I’m not that good at explaining things
Every time I press and release the shift key, the sound effect gets louder and starts printing 2,3,4,5,6… times on the console. so the sound does not increase spontaneously in the video, it happens because I press and release the sprint key.
Well, I made a footstep system as well and if you’re using animation events with a print statement it will print the detected material into Output a lot of times so this isn’t a problem right?
Hang on you keep creating a new sound inside of the character which is cloned depending on the material no wonder they overlap. You have so many audios that are created inside of the character so they start overlapping. My guess is to create one sound that changes it’s sound Id depending on the floormaterial
local Materials = {
[Enum.Material.Grass] = "rbxassetid://7003103812",
[Enum.Material.Sand] = "rbxassetid://9126733408",
[Enum.Material.Snow] = "rbxassetid://9126732128",
[Enum.Material.Pavement] = "rbxassetid://2599401908",
[Enum.Material.Pavement] = "rbxassetid://2599401908",
[Enum.Material.WoodPlanks] = "rbxassetid://9126931417"}
--Variables:
local Player = game.Players.LocalPlayer
local Character = Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local RootPart = Character:WaitForChild("HumanoidRootPart")
local Animator = Humanoid:WaitForChild("Animator")
--Functions:
local function UpdateFootstep()
local RunningSound = RootPart:FindFirstChild("Running")
if RunningSound then
RunningSound:Destroy()
end
local FloorMaterial = Humanoid.FloorMaterial
local FootstepSFX = Instance.new("Sound")
FootstepSFX.Parent = script
FootstepSFX.SoundId = Materials[FloorMaterial]
if Humanoid.WalkSpeed > 16 then
FootstepSFX.Volume = 1
else
FootstepSFX.Volume = 0.5
end
FootstepSFX:Play()
end
--Main:
Humanoid.AnimationPlayed:Connect(function(Track)
if Track.Name == "RunAnimation" or Track.Name == "WalkAnim" then
Track:GetMarkerReachedSignal("Footstep"):Connect(function()
UpdateFootstep()
end)
end
end)
The main problem here is that the function “Character.Humanoid.AnimationPlayed:Connect(function(track)” works in more than one cut at the same time
Yes and in your code you clone a sound and put it into the character. And the script starts to clone many sounds to the point that they overlap. You should use one sound that changes it’s Sound Id depending on the Humanoid’s FloorMaterial
Now when I walk normally nothing ever happens, no sound popping, no problem with the prints on the console. But every time I press shift both the sound gets louder and the print in the consol increases by 1 by 1.