Im working on a car traffic system where cars will line up on nodes and if they reach the final node they are destroyed and as cars line up it checks if the previous node is empty and spawns a car there but Im really confused and I’ve been trying to get it to work for hours and I also keep getting a tweenservice null
error
Error:
Server script that handles traffic:
local Control = require(script.VehicleControl)
local CarsFolder = game.Workspace.Cars
local RoadRoots = game.Workspace.RoadRoots
local nodes = {}
local function CheckNode(node)
for _, car in pairs(CarsFolder:GetChildren()) do
if car.CurrentNode.Value == node then
return car
end
end
end
local function GetCar(node)
for _, car in pairs(CarsFolder:GetChildren()) do
if car.CurrentNode.Value == node then
return car
end
end
end
local function Sort(a, b)
return tonumber(a.Name) < tonumber(b.Name)
end
for i, node in ipairs(RoadRoots.Queue:GetChildren()) do
table.insert(nodes, node)
end
table.sort(nodes, Sort)
local function CarThing(car)
if car then
car.CurrentNode.Changed:Connect(function()
local NextNode = nodes[tonumber(car.CurrentNode.Value.Name) + 1]
local PreviousNode = nodes[tonumber(car.CurrentNode.Value.Name) - 1]
local AdjecantCar = GetCar(NextNode)
if tonumber(car.CurrentNode.Value.Name) == 5 then
Control.Discard(car, nodes[5])
end
if CheckNode(NextNode) == nil then
Control.UpdateNode(car, NextNode)
else
CheckNode(NextNode):Destroy()
end
if CheckNode(nodes[PreviousNode]) == nil then
Control.Spawn(nodes[PreviousNode])
else
CheckNode(nodes[PreviousNode]):Destroy()
end
end)
else
return
end
end
CarsFolder.ChildAdded:Connect(CarThing)
task.wait(5)
Control.Spawn(nodes[4])
Module script that makes effects for the cars.
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TWEEN_INFO = {2, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, 0, false}
local Cars = ServerStorage.Cars
local ReplicatedModules = ReplicatedStorage.ReplicatedModules
local LocalisedTween = require(ReplicatedModules.LocalTween)
local function FadeVehicle(car, transparency)
for _, object in pairs(car:GetDescendants()) do
if (object:IsA("BasePart") or object:IsA("MeshPart")) and object.Name ~= "Root" then
object.Transparency = 1 - transparency
if object.Name ~= "Window" then
LocalisedTween.Tween(object, TWEEN_INFO, {Transparency = transparency})
else
if transparency == 1 then
LocalisedTween.Tween(object, TWEEN_INFO, {Transparency = 1})
else
LocalisedTween.Tween(object, TWEEN_INFO, {Transparency = 0.7})
end
end
end
end
end
local Vehicle = {}
function Vehicle.Spawn(EntryNode)
if EntryNode then
local NewCar = Cars.PlaceholderCar:Clone()
NewCar.Parent = game.Workspace.Cars
NewCar.PrimaryPart.CFrame = EntryNode.CFrame + Vector3.new(0, 0, 20)
wait(0.1) -- wait for it to load
LocalisedTween.Tween(NewCar.PrimaryPart, TWEEN_INFO, {CFrame = EntryNode.CFrame})
FadeVehicle(NewCar, 0)
task.wait(3)
NewCar.CurrentNode.Value = EntryNode
end
end
function Vehicle.Discard(Car, FinalNode)
if Car and FinalNode then
LocalisedTween.Tween(Car.PrimaryPart, TWEEN_INFO, {CFrame = FinalNode.CFrame - Vector3.new(0, 0, 20)})
FadeVehicle(Car, 1)
task.wait(2)
Car:Destroy()
end
end
function Vehicle.UpdateNode(Car, Node)
if Car and Node then
LocalisedTween.Tween(Car.PrimaryPart, TWEEN_INFO, {CFrame = Node.CFrame})
task.wait(3)
Car.CurrentNode.Value = Node
end
end
return Vehicle
Nodes