What’s supposed to happen
Code runs once, then the next “cycle” occurs where the same exact behaviour is supposed to be replicated again.
What actually happens
Decel, a sound object which is integral to the script, gets deleted for no apparent reason, which causes the script to be unable to find the object and spit out " Decel is not a valid member of Part “Workspace.Train1”.
The Code
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 dooropen = "rbxassetid://14261571781"
local doorclose= "rbxassetid://14261585755"
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 = game.Workspace.Train1.Decel
firstAudio.Parent = train
firstAudio.SoundId = decelerationAudioId
firstAudio:Play()
-- Start the looped audio in a separate thread
local loopedAudio = game.Workspace.Train1.IDLE
loopedAudio.Parent = train
loopedAudio.SoundId = loopedAudioId
loopedAudio.Looped = true
loopedAudio:Play()
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)
-- Play the deceleration audio from 0:16 to 0:37
-- Stop the train at the station
train.AssemblyLinearVelocity = Vector3.new()
wait(1)
local firstAudio = game.Workspace.Train1.OpenDoors
firstAudio.Parent = train
firstAudio.SoundId = dooropen
firstAudio:Play()
wait (35)
local firstAudio = game.Workspace.Train1.CloseDoors
firstAudio.Parent = train
firstAudio.SoundId = doorclose
firstAudio:Play()
-- Wait for 40 seconds
wait(8)
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
-- Play the acceleration audio from 0:15 to the end
-- Stop at the destination
train.AssemblyLinearVelocity = Vector3.new()
train.Position = Vector3.new(-360.501, 5.335, 7.726)
end
while true do
wait(100)
trainCycle()
wait(234) – Adjust the delay between cycles (in seconds) as needed
local firstAudio = game.Workspace.Train1.Decel
firstAudio.Parent = game.Workspace.Train1
firstAudio.SoundId = 14252924361
firstAudio:Play()
end`
Location
Script is located in game.StarterPlayer.StarterPlayerScripts
Sound object is located in game.workspace.Train1