Sound not wanting to loop twice

Hello,

So I got this script for my horror game where when you touch the trigger (red brick), a ball comes rolling down out of nowhere from around the corner and a wedge plays a clown honking sound that’s supposed to loop twice before ending the script. I used the API and found something that related to looping sounds a certain amount of times but I couldn’t get it to work with my code which sort of broke it. What’s weird is that I’m not getting any type of errors in the output hinting towards anything which makes things a bit harder.

Edit: By the way, the script is inside the ball in workspace.

Video:

Code:

local trigger = script.Parent.Parent.trigger
local Debounce = false
local ball = script.Parent
local honk = script.Parent.Parent.Wedge.honk
local function loopNTimes(sound, numberOfLoops)

trigger.Touched:Connect(function(hit)
	if not Debounce then
			if hit.Parent:FindFirstChild("Humanoid") then
				Debounce = true
			    ball.Anchored = false
				wait(1)
				honk.Looped = true
				local connection = nil
				connection = honk.DidLoop:Connect(function(soundId, numOfTimesLooped)
				if numOfTimesLooped >= 2 then
				connection:Disconnect()
				honk:Stop()
			    Debounce = false
			    script:Destroy()
					end
				end)
			end
		end
	end)
end

I’d appreciate any explanation!

how are you getting your sound id thru a remote event? or something else im not exactly sure how the problem most likely is there.

Not sure, I used some example from the API and tried implementing but didn’t work out well. I don’t really specialize in scripting to know right away.

that is probably your problem, I would try to rewrite the code making a variable of the sound and using that to loop thru it, or you could just play it twice. also instead of destroying the script you would want to destroy the sound that could help with it also.

1 Like

Update:

local trigger = script.Parent.Parent.trigger
local Debounce = false
local ball = script.Parent
local honk = script.Parent.Parent.Wedge.honk

trigger.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		if not Debounce then
			Debounce = true
			ball.Anchored = false
			honk:Play()
			honk.Ended:Wait()
			honk:Stop()
			honk:Play()
			honk.Ended:Wait()
			honk:Stop()
			script:Destroy()
		end
	end
end)

LOL I just realized that the main problem was that the sound was set to Looped by default without noticing. I just unchecked it and things worked now.

glad to help i thought it was in the sound cause your code seemed fine

1 Like

Yup, thank you so much for the suggestion on changing the code a bit!