I want the intro to play, then switch to looping the ain track then when the end is touched, play the outro. its for a chase sequence
if script:IsDescendantOf(game.Workspace) then
local repeatSound = true
local triggered = false
-- Function to repeat the MAIN audio
local function repeatAudio(sound)
while repeatSound do
task.wait(0.2)
-- Check if the sound has reached the end point
if sound.TimePosition >= 60 then
print("MAIN sound looped")
sound.TimePosition = 7
end
end
-- Stop the MAIN sound and play the OUTRO
print("Stopping MAIN and playing OUTRO")
sound:Stop()
script.OUTRO:Play()
end
-- Function to handle the STOP event
script.STOP.Value.Touched:Connect(function()
print("STOP part touched")
repeatSound = false
end)
-- Function to handle the START event
script.START.Value.Touched:Connect(function()
if not triggered then
triggered = true
print("START part touched, playing INTRO")
script.INTRO:Play()
script.INTRO.Stopped:Connect(function()
print("INTRO stopped, playing MAIN")
script.MAIN:Play()
-- Start the repeatAudio function in a separate thread
task.spawn(function()
repeatAudio(script.MAIN)
end)
end)
end
end)
end
but currently its not playing the MAIN track. its just playing the intro… then nothing. please help!
The .Stopped event will only fire if a script runs INTRO:Stop() on the track. What I think you meant to use is .Ended, which will fire once the sound finishes. You can read more about .Ended and .Stopped here.
So if you change the line of code I mentioned above into: script.INTRO.Ended:Connect(function()
I believe it will work fine!
I would also recommend doing sound.Looped = true instead of creating a function, but if the function is needed then feel free to use it.
repeat
task.wait(0.2)
-- Check if the sound has reached the end point
if sound.TimePosition >= 60 then
print("MAIN sound looped")
sound.TimePosition = 7
end
until not repeatSound
if script:IsDescendantOf(game.Workspace) then
local repeatSound = true
local triggered = false
-- Function to repeat the MAIN audio
function repeatAudio(sound)
repeat
task.wait(0.2)
-- Check if the sound has reached the end point
if sound.TimePosition >= 60 then
print("MAIN sound looped")
sound.TimePosition = 7
end
until not repeatSound
-- Stop the MAIN sound and play the OUTRO
print("Stopping MAIN and playing OUTRO")
sound:Stop()
script.OUTRO:Play()
end
-- Function to handle the STOP event
script.STOP.Value.Touched:Connect(function(hit)
if hit:FindFirstChild("Humanoid") then
repeatSound = false
end
end)
-- Function to handle the START event
script.START.Value.Touched:Connect(function()
if not triggered then
triggered = true
print("START part touched, playing INTRO")
script.INTRO:Play()
script.INTRO.Ended:Connect(function()
print("INTRO stopped, playing MAIN")
script.MAIN:Play()
-- Start the repeatAudio function in a separate thread
repeatAudio(script.MAIN)
end)
end
end)
end
Why are you doing this all inside the first function? Get rid of it and just set your repeatSound and triggered variables first. You can’t check to see if the script is a descendant of workspace because if it isn’t then it can’t run that check.
You use triggered as a debounce to keep both Touched events from firing multiple times when a player touches a Part but you NEVER set it back to false, and you NEVER check to see if it’s true in the other functions which is what it’s designed to do.