i have this module right here which handles the waypoint stuff
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local CarController = require(ReplicatedStorage.Common.CarController)
local AI = {}
local Static = {}
--[[
0 - Stop
1 - Proceed
]]
local Signals = {
['Institutional white'] = 1;
['Really blue'] = 1;
['Really black'] = 0;
}
function AI:init(car: Model)
return setmetatable({
car = car;
controller = CarController.new(car);
range = 10
}, {__index = Static})
end
function Static:cycle(waypoints: {BasePart}, start: number)
-- Increment safely
local function increment(n)
return (n+1) % (#waypoints:GetChildren())
end
-- The current index
local index = start
-- Get the current waypoint and the next waypoint
local waypoint = waypoints:FindFirstChild(index)
local target = waypoints:FindFirstChild(increment(index))
-- Pivot the car to the current waypoint
self.car:PivotTo(waypoint.CFrame)
return RunService.Heartbeat:Connect(function()
-- Get the signal
local signal = Signals[waypoint.BrickColor.Name]
-- Get the distance from target to the car
local dist = (target.Position - self.car.Sensors.Front.Position).Magnitude
-- If reached a certain distance, increment the index
if dist < self.range then
index = increment(index)
end
if signal == 0 then
self.controller:Accelerate(0)
elseif signal == 1 then
self.controller:MoveTo(target.Position)
end
-- Update the waypoint and the target
waypoint = waypoints:FindFirstChild(index)
target = waypoints:FindFirstChild(increment(index))
end)
end
return AI
and because of i was planning it to have intersections and traffics, i want it to decide on which waypoint to waypoint to go
how can i do that?