I need help on this one about i,v paris

for i, v in pairs(game.Workspace.Cars:GetChildren()) do
local car = v.CarName
if (player.Character.HumanoidRootPart.Position - car.Parent.Chassis.VehicleSeat.Position).magnitude < 12 then
print(“Repair”)

How do i make it only pick one car because when 2 car surrounds me it repair 2 car i just need it to repair only one car

1 Like

You will need to break the loop for this

for i, v in game.Workspace.Cars:GetChildren() do
 local car = v.CarName
 if (player.Character.HumanoidRootPart.Position-car.Parent.Chassis.VehicleSeat.Position).Magnitude > 12 then
   print("Repair")
   break --Stops the loop
   --other code
 end 
end

The break keyword is used to stop a loop, whether it may be a while loop or a for loop.
By the way, pairs and ipairs are no longer required for loops that iterate through a table’s elements, you can just do for i, v in someTable do and it’ll do its job.

3 Likes

This is interesting. It’s my first time hearing this, and I thought this was either a deprecated and extremely slow way to iterate through tables or didn’t exist at all. Seems like It’s more performant than most iterators.
image

1 Like