So lets say we have a train. It is moving along with the train cars. Its moving fine. You know, like Jailbreak.
The problem is… When i test the train on client and server, when it lags, the train cars on the back will start separating. Like someone pulled the pin off the train cars.
First of all before moderators send messages to me, I apologize if I do not include any code or screenshots because I am mobile right now. Soon when I get back on my pc, I will edit this topic the soonest including screenshots and code.
UPDATE 1/30/2020:
That is the picture of the train cars separating and sometimes being too close together.
Here is the script that requires the module
wait(8 + math.random())
--Variables
local Workspace = game:GetService("Workspace")
local ServerStorage = game:GetService("ServerStorage")
local Train = ServerStorage.Train
local Train = Train.Train
local TrainClass = require(Train.TrainClass)
local TrainSetup = TrainClass.new(Train,Workspace.Nodes)
TrainSetup:Setup()
wait(workspace.Train:WaitForChild("Train"))
script.Disabled = true
Parented in serverscriptservice
Here is the train mechanics code (That welds all train cars and move them):
local TrainSuperClass = {}
TrainSuperClass.__index = TrainSuperClass
local Workspace = game:GetService("Workspace")
local TweenService = game:GetService("TweenService")
local RunService = game:GetService("RunService")
local ServerStorage = game:GetService("ServerStorage")
local Carriages = ServerStorage.Carriages
local RunService = game:GetService("RunService")
local EasingModule = require(game.ReplicatedStorage.MainModule)
function TrainSuperClass.new(TrainModel,Nodes)
local Train = {}
setmetatable(Train,TrainSuperClass)
Train.Nodes = Nodes
Train.Dummy = TrainModel
Train.Node = 0
Train.LasCarriage = nil
Train.BogeyOffset = Train.Dummy.BackBogeyRoot
return Train
end
function TrainSuperClass:Setup()
print("Train has started!")
self.Train = self.Dummy:Clone()
self.Train.Parent = Workspace.Train
self:Move()
end
function TrainSuperClass:GetTime(Distance)
return Distance / self.Speed
end
function TrainSuperClass:Carriages(Node)
local Carriage = Carriages:GetChildren()[math.random(1,#Carriages:GetChildren())]:Clone()
local All = Carriage:GetChildren()
for A = 1,#All do
if (All[A].Name~= self.Train.PrimaryPart and All[A]:IsA("BasePart")) then
local NewWeld = Instance.new("Weld")
NewWeld.Name = "Weld"
NewWeld.Part0,NewWeld.Part1 = All[A],Carriage.PrimaryPart
NewWeld.C0 = All[A].CFrame:inverse()
NewWeld.C1 = Carriage.PrimaryPart.CFrame:inverse()
NewWeld.Parent = Carriage.PrimaryPart
end
end
if not (self.LastCarriage) then
self.LastCarriage = self.Train
end
local Range = NumberRange.new(1,2)
Carriage:SetPrimaryPartCFrame(self.LastCarriage:GetPrimaryPartCFrame() * CFrame.new(0,0,Carriage.PrimaryPart.Size.Z+2))
self.LastCarriage = Carriage
Carriage.Parent = Workspace.Train
spawn(function()
wait()
self:MoveCarriage(Node,Carriage)
end)
end
function TrainSuperClass:MoveCarriage(NodeNum,Carriage)
local Node = self.Nodes:FindFirstChild("Node"..NodeNum)
if not (Node) then
Carriage:Destroy()
return
end
local Distance = (Carriage.PrimaryPart.CFrame.p - Node.CFrame.p).Magnitude
local BetweenDistance = (Carriage.PrimaryPart.CFrame.p - self.LastCarriage.PrimaryPart.CFrame.p).Magnitude
local Time = self:GetTime(Distance)
local TweenInfo = TweenInfo.new(
Time,
Enum.EasingStyle.Linear,
Enum.EasingDirection.Out,
0,
false,
0
)
local Range = NumberRange.new(1.5,2)
TweenService:Create(Carriage.PrimaryPart,TweenInfo,{CFrame = Node.CFrame + Vector3.new(0,Carriage.PrimaryPart.Size.Y/3,Carriage.PrimaryPart.CFrame.LookVector/2)}):Play()
wait(Time)
self:MoveCarriage(NodeNum + 1,Carriage)
end
function TrainSuperClass:Move()
self.Node = self.Node + 1
local Node = self.Nodes:FindFirstChild("Node"..self.Node)
if not(Node) then
self:Stop()
return
end
if self.Node == 1 then
local All = self.Train:GetChildren()
for A = 1,#All do
if (All[A].Name~= self.Train.PrimaryPart and All[A]:IsA("BasePart")) then
local NewWeld = Instance.new("Weld")
NewWeld.Name = "Weld"
NewWeld.Part0,NewWeld.Part1 = All[A],self.Train.PrimaryPart
NewWeld.C0 = All[A].CFrame:inverse()
NewWeld.C1 = self.Train.PrimaryPart.CFrame:inverse()
NewWeld.Parent = self.Train.PrimaryPart
end
end
wait()
self.Train:SetPrimaryPartCFrame(Node.CFrame + Vector3.new(0,self.Train.PrimaryPart.Size.Y/2,0))
for i = self.CarriagesCount,1,-1 do
self:Carriages(self.Node)
end
self:Move()
return
end
local Distance = (self.Train.PrimaryPart.CFrame.p - Node.CFrame.p).Magnitude
local Time = self:GetTime(Distance)
local TweenInfo = TweenInfo.new(
Time,
Enum.EasingStyle.Linear,
Enum.EasingDirection.Out,
0,
false,
0
)
local Range = NumberRange.new(1.5,2)
TweenService:Create(self.Train.PrimaryPart,TweenInfo,{CFrame = Node.CFrame + Vector3.new(0,self.Train.PrimaryPart.Size.Y/2,0)}):Play()
wait(Time)
self:Move()
if (self.Node == 10) then
print("Node is ten yen....")
end
end
function TrainSuperClass:Stop()
self.LastCarriage = nil
self.Train:Destroy()
self.Train.Parent = nil
self.Node = 0
print("Train has finished! Starting setup...")
wait(14)
self:Setup()
end
return TrainSuperClass
Parented in serverscriptservice in SuperClasses folder. Its a folder made by me so ignore it.