‘Traffic AI’: Sensor to stop vehicle doesn’t want to work

Turns out that a few days ago, I worked on a kind of system that moves a vehicle model between several checkpoints, trying to simulate a basic traffic system. It works great. But when I tried to make the system to stop the vehicle if there’s something in front of it, it didn’t work. It’s complex; it works but it doesn’t work:

If I place a block in front of the vehicle, the sensor doesn’t seem to detect it, and the vehicle continues moving despite having the script. It doesn’t even collide, even though both have ‘CanCollide’ enabled.

On the other hand, if I play with my character and stand in the middle, it works, but not with other ‘AI’ vehicles or simple parts.

It doesn’t finish here; if I run the game and drag a part without ANCHORING it, the system works, but I don’t want it to be that way. It doesn’t make sense…

Here are details about what’s happening to me and the script of the ‘AI’:


“base” is the PrimaryPart.

Here’s the AI script where it can search and move through the checkpoints:

– Navegador de destinos continuos (sin pathfinding y con humanoid) –
local RunService = game:GetService(“RunService”)

local vehicle = script.Parent.Parent
local humanoid = vehicle.valores:FindFirstChildOfClass(“Humanoid”)
local checkpointsFolder = game.Workspace:FindFirstChild(“destinos”)

local arrivalDistance = 5
local turnSpeed = 0.1

local vehicleSeat = vehicle:FindFirstChild(“asiento”)

local function moveToCheckpoint(destination)
local targetPosition = destination.Position
local direction = (targetPosition - vehicle.PrimaryPart.Position).unit
local distance = (targetPosition - vehicle.PrimaryPart.Position).Magnitude

while distance > arrivalDistance do
direction = (targetPosition - vehicle.PrimaryPart.Position).unit
local moveVector = direction * (humanoid.WalkSpeed * RunService.Heartbeat:Wait())

  local currentCFrame = vehicle.PrimaryPart.CFrame
  local targetCFrame = CFrame.lookAt(vehicle.PrimaryPart.Position, vehicle.PrimaryPart.Position + direction)
  local newCFrame = currentCFrame:Lerp(targetCFrame, turnSpeed)

  local moveDistance = moveVector.Magnitude
  local moveCFrame = newCFrame + (direction * moveDistance)
  vehicle:SetPrimaryPartCFrame(moveCFrame)

  if vehicleSeat then
  	vehicleSeat.Steer = direction.X
  	vehicleSeat.Throttle = direction.Z
  end

  distance = (targetPosition - vehicle.PrimaryPart.Position).Magnitude
  RunService.Heartbeat:Wait()

end
end

local checkpoints = {}
if checkpointsFolder then
for _, checkpoint in ipairs(checkpointsFolder:GetChildren()) do
table.insert(checkpoints, checkpoint)
end
table.sort(checkpoints, function(a, b) return tonumber(a.Name) < tonumber(b.Name) end)
end

while true do
for _, checkpoint in ipairs(checkpoints) do
moveToCheckpoint(checkpoint)
end
end

And here’s the sensor script:


local veh = script.Parent.Parent
local sensor = veh.sensor
local hum = veh.valores:WaitForChild(“general”)

sensor.Touched:Connect(function(hit)
if hit.Name == “Part” then
hum.WalkSpeed = 0
end

end)

Previously, the script was built with the ‘Pathfinding’ service, but the problem was the same. I changed it to this one and added the ‘Humanoid’ because previously the speed was a ‘NumberValue’. The script to stop the vehicle was more complex before, but since it had that error, I switched it to this basic one. If you can resolve this issue, I would really appreciate it.
Thanks for reading!
Cheers from Uruguay!

ROBLOX raycasts are known to be buggy and not work all the time.

Also for vehicles you should use shapecasts not raycasts

1 Like

no idea where you got that from but raycasts are reliable unless the creator is the one having a miscalculation with direction/origin. Any gun system uses raycasting/ whatever other method that is. Prjectile systems may use raycasting most do.

2 Likes

I already solved the problem, thanks for watching!