My game has busses in it that player can unlock. These busses have 2 very simple tweens that are played and looped when they are unlocked.
The tweens for these busses are run on the client side and work perfectly, however a problem comes forward when the player that owns the busses leaves. I want the tween for these specific busses to be completely destroyed for every client, but I cant seem to figure it out.
Code is here:
Local Script. Located in StarterPlayerScripts
-- Function BusTween gets called when the RemoteEvent "BusTween" is called by the server (So a player bought a bus and every client
-- will start the tween.)
local function BusTween(Bus, Owner)
local TweenService = game:GetService("TweenService")
-- First tween: Bus rides away.
local function RideAway(StartPart, EndPart)
local TweeningInformation = TweenInfo.new(
1.5, -- Length
Enum.EasingStyle.Quad, -- Style
Enum.EasingDirection.In,
0,
false,
0
)
local PartProperties = {
Transparency = 1;
Position = EndPart.Position
}
local Tween = TweenService:Create(StartPart, TweeningInformation, PartProperties)
Tween:Play()
-- When player that owns the busses leaves,
Players.PlayerRemoving:Connect(function(PlayerLeft)
if PlayerLeft == Owner then
Tween:Destroy()
end
end)
return Tween
end
local function RideBack(StartPart, EndPart)
local TweeningInformation = TweenInfo.new(
1.5, -- Length
Enum.EasingStyle.Quad, -- Style
Enum.EasingDirection.Out,
0,
false,
0
)
local PartProperties = {
Transparency = 0;
Position = EndPart.Position
}
local Tween = TweenService:Create(StartPart, TweeningInformation, PartProperties)
Tween:Play()
Players.PlayerRemoving:Connect(function(PlayerLeft)
if PlayerLeft == Owner then
Tween:Destroy()
end
end)
end
while true do
wait(8)
-- Bus consists of multiple meshes, so for every part a tween is made to their pre defined location (FrontBusTween)
for _, SmallPart in pairs(Bus:GetChildren()) do
if SmallPart:IsA("BasePart") then
RideAway(SmallPart, Bus.FrontBusTween[SmallPart.Name]).Completed:Connect(function()
-- Dont pay attention to this.
SmallPart.Position = SmallPart.Position + Vector3.new(0, -100, 0)
end)
end
end
wait(math.random(6, 10))
-- Bus consists of multiple meshes, so for every part a tween is made to their pre defined location (BackBusTween)
for _, SmallPart in pairs(Bus:GetChildren()) do
if SmallPart:IsA("BasePart") then
SmallPart.Position = Bus.BackBusTween[SmallPart.Name].Position
RideBack(SmallPart, Bus.MiddleBusTween[SmallPart.Name])
end
end
end
end
game.ReplicatedStorage.BusTween.OnClientEvent:Connect(
BusTween
)
Module Script, Located in ServerScriptService
function InteractModule.Bus(Part, TycoonModel)
-- This code is run everytime a player in a Tycoon unlocks a bus.
game.ReplicatedStorage.BusTween:FireAllClients(Part, TycoonModel.Values.OwnerValue.Value)
end```