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
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.
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.