Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I loop this script over and over again?

while true do
local TrainSuperClass = {}
TrainSuperClass.__index = TrainSuperClass

local TweenService = game:GetService("TweenService")


function TrainSuperClass.new(TrainModel,Nodes)
	local Train = {}
	
	setmetatable(Train,TrainSuperClass)
	
	Train.Nodes = Nodes
	Train.TestModel = TrainModel
	Train.Node = 1
	Train.LastCarriage = nil
	
	return Train
	
end

function TrainSuperClass:Setup()
	
	if not(self.Nodes:FindFirstChild("Node"..self.Node)) then
		warn("Cannot Find node"..self.Node)
		return 
	end
	
	self.Train = self.TestModel:Clone()
	self.Train.Parent = workspace
	
	local Children = self.Train:GetChildren()
	
	for i = 1,#Children do
		if (Children[i] ~= self.Train.PrimaryPart) then
			local Weld = Instance.new("Weld")
			Weld.Part0 = Children[i]
			Weld.Part1 = self.Train.PrimaryPart
			Weld.C0 = Children[i].CFrame:inverse()
			Weld.C1 = self.Train.PrimaryPart.CFrame:inverse()
			Weld.Parent = self.Train.PrimaryPart
		end
	end
	
	wait()
	self.Train:SetPrimaryPartCFrame(self.Nodes:FindFirstChild("Node"..self.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()
end

function TrainSuperClass:GetTime(Distance)
	return Distance / self.Speed
end

function TrainSuperClass:Carriages(Node)
	local Carriage = game:GetService("ServerScriptService").Train.Carriages:GetChildren()[math.random(1,#game:GetService("ServerScriptService").Train.Carriages:GetChildren())]:Clone()
	
	local All = Carriage:GetChildren()
	
	for i = 1,#All do
		if (All[i] ~= self.Train.PrimaryPart) then
			local Weld = Instance.new("Weld")
			Weld.Part0 = All[i]
			Weld.Part1 = Carriage.PrimaryPart
			Weld.C0 = All[i].CFrame:inverse()
			Weld.C1 = Carriage.PrimaryPart.CFrame:inverse()
			Weld.Parent = Carriage.PrimaryPart
		end
	end
	
	if not (self.LastCarriage) then
		self.LastCarriage = self.Train
	end
	
	Carriage:SetPrimaryPartCFrame(self.LastCarriage:GetPrimaryPartCFrame() * CFrame.new(0,0,-Carriage.PrimaryPart.Size.Z+2.5))
	
	self.LastCarriage = Carriage
	Carriage.Parent = workspace
	
	spawn(function()
		self:MoveCarriage(Node + 1,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.Position - Node.Position).Magnitude
	
	local Time = self:GetTime(Distance)
	
	local TweenInfo = TweenInfo.new(
		Time,
		Enum.EasingStyle.Linear,
		Enum.EasingDirection.Out,
		0,
		false,
		0
	)
	
	TweenService:Create(Carriage.PrimaryPart,TweenInfo,{CFrame = Node.CFrame + Vector3.new(0,Carriage.PrimaryPart.Size.Y/2,0)}):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
	
	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
	)
	
	TweenService:Create(self.Train.PrimaryPart,TweenInfo,{CFrame = Node.CFrame + Vector3.new(0,self.Train.PrimaryPart.Size.Y/2,0)}):Play()
	
	wait(Time)
	
	self:Move()
end

function TrainSuperClass:Stop()
	self.Node = 1
	self.Train:Destroy()
end

return TrainSuperClass

2 comments

0

This script doesn't do anything.

0

It follows a track of nodes.

1 answer

This isn't a script, it's a module. You would put that code inside of a ModuleScript and require it.

If you found this answer helpful, do mark it as one.

This discussion is preserved as it was. New answers, comments, and votes are closed.