Hi, I have a script here which is supposed to play a declaration sound and idle sound. While these play as intended on the first cycle (first run), when it loops, it doesn’t play the audio:
local function trainCycle()
local train = game.Workspace.Train1
local startPart = game.Workspace.start_train
local stationPart = game.Workspace.train_station
local stopPart = game.Workspace.train_stop
local speed = 40
local SoundService = game:GetService("SoundService")
local decelerationAudioId = "rbxassetid://14252924361"
local accelerationAudioId = "rbxassetid://14253510273"
local loopedAudioId = "rbxassetid://14253124958"
local function moveTrainSmoothly(startPos, endPos, duration)
local initialPosition = train.Position
local t = 0
game:GetService("TweenService"):Create(train, TweenInfo.new(duration), {
Position = endPos
}):Play()
while t < duration do
t = t + wait()
train.Position = initialPosition:Lerp(endPos, t / duration)
end
train.Position = endPos
end
local function accelerateTrainSmoothly(startPos, endPos, duration)
local initialPosition = train.Position
local initialVelocity = train.AssemblyLinearVelocity
-- Calculate the desired velocity to reach the end position in the given duration
local desiredVelocity = (endPos - startPos).unit * (endPos - startPos).magnitude / duration
game:GetService("TweenService"):Create(train, TweenInfo.new(duration), {
AssemblyLinearVelocity = desiredVelocity
}):Play()
local t = 0
while t < duration do
t = t + wait()
train.AssemblyLinearVelocity = initialVelocity:Lerp(desiredVelocity, t / duration)
train.Position = initialPosition + train.AssemblyLinearVelocity * t
end
train.AssemblyLinearVelocity = desiredVelocity
train.Position = endPos
end
-- Function to play audio starting at a specific time and ending at a specific time
local function playAudio(audioId, startTime, endTime)
local sound = Instance.new("Sound")
sound.Parent = train
sound.SoundId = audioId
sound.TimePosition = startTime
sound:Play()
while wait() do
if sound.TimePosition >= endTime then
sound:Stop()
break
end
end
end
-- Play the first audio immediately
local firstAudio = Instance.new("Sound")
firstAudio.Parent = train
firstAudio.SoundId = decelerationAudioId
firstAudio:Play()
-- Start the looped audio in a separate thread
local loopedAudio = Instance.new("Sound")
loopedAudio.Parent = train
loopedAudio.SoundId = loopedAudioId
loopedAudio.Looped = true
loopedAudio:Play()
while true do
wait(20) -- Wait for 20 seconds before starting the cycle
-- Decelerate from start to station
local decelerationDuration = 10 -- Adjust the duration to control deceleration
moveTrainSmoothly(startPart.Position, stationPart.Position, decelerationDuration)
-- Stop the train at the station
train.AssemblyLinearVelocity = Vector3.new()
-- Wait for 40 seconds
wait(10)
game.ReplicatedStorage.itleave:FireServer()
-- Accelerate from station to stop
local accelerationDuration = 20 -- Adjust the duration to control acceleration
accelerateTrainSmoothly(stationPart.Position, stopPart.Position, accelerationDuration)
-- Stop the looped audio and wait for train to accelerate again
loopedAudio:Stop()
for _, sound in ipairs(train:GetChildren()) do
if sound:IsA("Sound") then
sound:Stop()
end
end
-- Stop at the destination
train.AssemblyLinearVelocity = Vector3.new()
-- Teleport back to start_train and wait for 3 minutes before starting the cycle again
train.Position = startPart.Position
wait(180) -- 3 minutes
end
end
trainCycle()