I made a script where there is a sound affect when you step on metal material. The sound keeps looping though, anyone know how to fix that?
Using a .Touched
event will run every time something touches it, so adding a debounce will ensure that it only goes off once.
local debounce = false
thing.Touched:Connect(function()
if not debounce then
debounce = true
sound:Play()
end
end
I’ve made something like this before, I made it a game and let anyone copy it, here is the link hope you learn something from it
I would put this at the end correct? Every time I attempt it errors. I have no idea why.
Could you show me the error you get in your output?
Oh, what I sent you was supposed to be the entire script alone. Also, when I used “thing” for thing.Touched and “sound” for sound:Play I was just using an example. In this case, “thing” would be the LeftFoot, and “sound” would be your sound object:
local char = script.Parent
local soundTable = {
["Metal"] = "rbxassetid://1083174031";
}
local debounce = false
local leftFoot = char:WaitForChild("LeftFoot")
leftFoot.Touched:Connect(function(hit)
if not debounce then
if hit.Parent ~= char and soundTable[hit.Material.Name] then
debounce = true
local sound = Instance.new("Sound")
sound.SoundId = soundTable[hit.Material.Name]
sound:Play()
Debris:AddItem(sound, 1)
wait(1) -- time you want to wait before the sound can play again when touched
debounce = false
end
end
end
I hope this helps
Thanks. I understand what I did wrong now
oh wait- It’s not working still.
I did make a few edits to my post. Also, this should be in a ServerScript located in StarterCharacterScripts. If it still does not work, show me any errors in the output window
There does not seem to be errors, but I still don’t hear the walking sound.
I placed it in a script, inside StarterCharacterScripts
I checked the dev console, Apparently the error is on line 7, next to function.
On your screen it says “Expected end…” on line number 9. Did you forget to add an “end?”
In my opinion, I would not use .Touched to detect when they take a step, instead, you can take the default Animate script and edit it to use GetMarkerReachedSignal
. What I did was downloaded the run and walk animation and just edited to add a marker, then I used GetMarkerReachedSignal
to detect when they took a step and played sounds accordingly.
I don’t think parenting a sound object to your left foot is a good idea, if you can’t hear it and there’s nothing wrong with the script itself, modify the volume property.
Still nothing. I added the end, and I checked the dev console. Output also for any other extra mistakes.
Send your entire script here please in a codeblock.
alright
local char = script.Parent
local soundTable = {
[“Metal”] = “rbxassetid://834851222”;
}
local debounce = false
local leftFoot = char:WaitForChild(“LeftFoot”)
leftFoot.Touched:Connect(function(hit)
if not debounce then
if hit.Parent ~= char and soundTable[hit.Material.Name] then
debounce = true
local sound = Instance.new(“Sound”)
sound.SoundId = soundTable[hit.Material.Name]
sound:Play()
Debris:AddItem(sound, 1)
wait(1) – time you want to wait before the sound can play again when touched
debounce = false
end
end
end
found the problem, the sound is not parented to anything. my bad
Where are you parenting this script to the player’s character?
Where are you parenting the sound now? Thought you did it before.
You’re playing the sound and using Debris to add it?
Looks very badly written with many flaws.
There is a property in the character’s humanoid callde FloorMaterial you can use it to detect what they are stepping on. There is no need for touch events.(pretty sure it works on terrain too)