So, I am trying to get sound in different areas in my game. The way I did this was to make a part, insert two scripts and put this in the first one:
script.Parent.Touched:connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") then game.Players[hit.Parent.Name].PlayerGui.Sound:Resume() end end)
And this in the second one:
script.Parent.Touched:connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") then game.Players[hit.Parent.Name].PlayerGui.Sound:Resume() end end)
After doing that, I pressed play, ran into the area, everything worked. I was happy with that until I loaded into a custom character that I had made, and when I used that to run into the area or part, it played the music as normal but caused tremendous lag. I couldn't hardly move, so I thought maybe the script was running a gazillion times and causing lag, so I added debounce to it. This is what the first script looks like after adding debounce:
local Touched = false script.Parent.Touched:connect(function(hit) if not Touched then Touched = true if hit.Parent:FindFirstChild("Humanoid") then game.Players[hit.Parent.Name].PlayerGui.Sound:Play() end end end)
I added debounce to both of them, although I don't see why it needs it at all. Loaded up my normal character, everything worked fine. Loaded with my custom character, horrible lag same as before. I don't understand what could be causing this, as the script shouldn't cause any lag at all.
Any advice would be greatly appreciated.
That's not the proper use of a debounce. Since the two if statements aren't connected, you are basically running the same laggy script, except that you are changing a local variable at the start of it.
My suggestion is to just check if the sound is playing through a property called "IsPlaying"
--- An Example if Sound.IsPlaying == false then Sound:Play() end
Hopefully this fixes your problem!