local runtime = game:GetService("RunService")
local char = script.Parent
local hrp = char.RightLowerLeg
local hum = char.Humanoid
local materials = script.Holder:WaitForChild("FootstepSounds")
materials.Parent = hrp
local jumpSounds = {
Wood = "woodjump",
WoodPlanks = "woodjump",
DiamondPlate = "metaljump",
Metal = "metaljump",
Mud = "rockjump",
Slate = "rockjump",
Rock = "rockjump",
Grass = "grassjump",
Fabric = "FabricJump",
Neutral = "neutraljump", -- Fallback for unlisted materials
}
local landingSounds = {
Wood = "woodplace",
WoodPlanks = "woodplace",
Metal = "metalplace",
DiamondPlate = "metalplace",
Rock = "rockplace",
Mud = "rockplace",
Slate = "rockplace",
Grass = "grassplace",
Fabric = "FabricJump",
Neutral = "neutralplace", -- Fallback for unlisted materials
}
local walking = false
local currentspeed = 0
local lastMaterial
local cachedGroundMaterial = nil
local isJumping = false
local soundDebounce = false -- Debounce to prevent repeated sound plays
local function getMaterial()
local floormat = hum.FloorMaterial
if not floormat then
return "Air"
end
local matstring = tostring(floormat):match("Enum.Material.(.+)")
return matstring or "Air"
end
hum.StateChanged:Connect(function(_, newState)
if newState == Enum.HumanoidStateType.Jumping and not soundDebounce then
soundDebounce = true
cachedGroundMaterial = getMaterial()
local jumpSoundName = jumpSounds[cachedGroundMaterial] or jumpSounds["Neutral"]
local jumpSound = materials:FindFirstChild(jumpSoundName)
if jumpSound then
jumpSound:Play()
end
isJumping = true
wait(0.1) -- Short debounce wait
soundDebounce = false
elseif newState == Enum.HumanoidStateType.Landed and isJumping and not soundDebounce then
soundDebounce = true
local landSoundName = landingSounds[cachedGroundMaterial] or landingSounds["Neutral"]
local landSound = materials:FindFirstChild(landSoundName)
if landSound then
landSound:Play()
end
isJumping = false
wait(0.1) -- Short debounce wait
soundDebounce = false
end
end)
hum.Running:Connect(function(speed)
currentspeed = speed
walking = speed > 1
end)
runtime.Heartbeat:Connect(function()
if walking then
local currentMaterial = getMaterial()
local materialSound = materials:FindFirstChild(currentMaterial) or materials:FindFirstChild("neutralplace")
if materialSound then
if lastMaterial and lastMaterial ~= currentMaterial and materials:FindFirstChild(lastMaterial) then
materials[lastMaterial].Playing = false
end
materialSound.PlaybackSpeed = math.max(currentspeed / 12, 0.5)
if not materialSound.Playing then
materialSound.Playing = true
end
lastMaterial = currentMaterial
else
print("Material sound not found for:", currentMaterial)
end
else
for _, sound in pairs(materials:GetChildren()) do
if sound.Playing then
sound.Playing = false
end
end
end
end)
zero errors, but jump sound is like 5 percent to play
If the jump sound only plays about 5% of the time, there are a few potential reasons for this inconsistent behavior. Here are some troubleshooting steps and adjustments you can make to improve the reliability of the jump sound:
Potential Issues and Solutions
Sound Debounce Timing:
Ensure that the debounce timing (wait(0.1)) is sufficient to allow the jump sound to play without getting interrupted. You might want to increase this duration to see if it has a positive effect.
Try using a task.wait() instead of wait(), as it can be more reliable in some cases.
State Change Timing:
The StateChanged event might be firing too quickly, resulting in missed opportunities to play the sound. Adding a slight delay before the sound plays can help mitigate this.
Material Detection Timing:
If getMaterial() is called too quickly after the jump state is triggered, it might return an incorrect or unexpected material. You could add a slight delay to give the physics engine time to update the character’s state and floor material.
Sound Playback Conditions:
Ensure that the sound objects are properly set up and loaded. If the sound objects are not correctly instantiated or are muted, they might not play as expected.
Check for Other Interference:
Ensure that no other scripts or game logic interfere with the jump sound playback (e.g., other scripts modifying sound properties or states).
Suggested Code Adjustments
Here’s an example of how to implement some of these suggestions:
lua
Copy code
hum.StateChanged:Connect(function(_, newState)
if newState == Enum.HumanoidStateType.Jumping and not soundDebounce then
soundDebounce = true
cachedGroundMaterial = getMaterial()
-- Adding a slight delay to allow for proper state updates
task.wait(0.05)
local jumpSound = jumpSoundCache[cachedGroundMaterial] or jumpSoundCache["Neutral"]
if jumpSound then
jumpSound:Play()
end
isJumping = true
-- Allowing a longer debounce time to ensure sounds don't overlap
task.wait(0.5)
soundDebounce = false
elseif newState == Enum.HumanoidStateType.Landed and isJumping and not soundDebounce then
soundDebounce = true
local landSound = landingSoundCache[cachedGroundMaterial] or landingSoundCache[cachedGroundMaterial]["Neutral"]
-- Adding a slight delay for landing sound
task.wait(0.05)
if landSound then
landSound:Play()
end
isJumping = false
task.wait(0.5)
soundDebounce = false
end
end)
Additional Tips
Testing in Different Scenarios:
Test your jump functionality on different surfaces to ensure the materials are correctly identified.
Jump continuously to see if the sound plays under different conditions.
Debugging Output:
Add print statements to log when the jump and land sounds are triggered. This can help identify if the conditions are met or if the sounds are not being called at all.
Make sure the sounds have appropriate volume levels and are set to play at the right distances so that they are audible during gameplay.
By making these adjustments and testing thoroughly, you should see improved performance in how often the jump sound plays. Let me know if this helps or if you have any other questions!