Why is it overlapping?

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?

video: Watch Desktop 2024.12.07 - 19.28.46.01 (online-video-cutter.com) | Streamable

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)

5 Likes

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 :sweat_smile:

5 Likes

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.

5 Likes

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?

5 Likes

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

5 Likes
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)

Here’s my code for my footstep system :slight_smile:

4 Likes

If you want to take anything from here feel free :slight_smile:

3 Likes

No, raycast doesn’t generate any sound, it just tells you what material the part under your foot has.

4 Likes

The main problem here is that the function “Character.Humanoid.AnimationPlayed:Connect(function(track)” works in more than one cut at the same time

srry function is not overlapping

3 Likes

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

3 Likes

hmmm well then I’ll try to use it

2 Likes

I don’t understand what you mean by:

3 Likes

forget about it. the function doesn’t overlap. misspoke

3 Likes

Feel free to use the code :slight_smile:
Has it worked?

3 Likes

wow same problem wait im sending video

3 Likes

Hang on I’ll be right back wait

2 Likes

Alright you can send the video now

3 Likes

Watch Desktop 2024.12.07 - 21.30.14.02 | Streamable here

3 Likes

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.

1 Like

Could you send me the code for the inputs or anything related?

1 Like