# Loop cars and check if they are in colshapes

## Recommended Posts

Hello, I have a table with 5 cuboids:

cuboid[1] = {x,y,z,10,10,10}

cuboid[2] = {x,y,z,10,10,10}

etc

and I wanna check if there is any vehicle on these cuboids, if no then break the table i tried it

```
for _,v in ipairs(getElementsByType('vehicle')) do
outputChatBox('checking from ' .. getVehicleName(v) .. ' perspective')
for i=1, 5 do
if isElementWithinColShape(v, cuboid[i]) then
outputChatBox(i .. ' taken by ' .. getVehicleName(v))
else
outputChatBox(i .. ' free')
end
end
end

```

So i spawn 2 cars at 2 cuboids, infernus at 1st, cheetah at 2nd

```checking from Cheetah perspective
1 free
2 taken by Cheetah
3 free
4 free
5 free
checking from Infernus perspective
1 taken by Infernus
2 free
3 free
4 free
5 free
```

the problem is that Cheetah sees that cuboid 1 is free while it's taken and infernus sees 1 is free

Could you help me?

this?:

```function isInAnyFiveCuboid()
local vehicles = getElementsByType("vehicle")
for k,v in ipairs(vehicles)do
if(isElementWithinColShape(v,cuboid[1]) or isElementWithinColShape(v,cuboid[2]) or
isElementWithinColShape(v,cuboid[3]) or isElementWithinColShape(v,cuboid[4]) or
isElementWithinColShape(v,cuboid[5])) then
outputChatBox(getVehicleName(v).."is in cuboid")
else
outputChatBox(getVehicleName(v).."is out cuboid")
end
end
end
```

Looks like it partially works. Now I wanted to make it that If a vehicle is in cuboid it adds +1 to the variable but it counts all the cars I have on the server

So if I have 5 cuboids for 5 parking places

cuboid[1]

cuboid[2]

cuboid[3]

cuboid[4]

cuboid[5]

And i have something like it:

```
(at top of the script) count = 0
local vehicles = getElementsByType("vehicle")
for k,v in ipairs(vehicles)do
if(isElementWithinColShape(v,cuboid[1]) or isElementWithinColShape(v,cuboid[2]) or
isElementWithinColShape(v,cuboid[3]) or isElementWithinColShape(v,cuboid[4]) or
isElementWithinColShape(v,cuboid[5])) then
count =count +1
else
createVehicle(foo)
end
end
```

And I spawn first car then count is 1, but when I spawn second count is 2, and... when I'm trying to spawn the third count is 4 because it's add +1 2x to variable.

It's hard to explain but I want it done so badly.

```count = 0
takenColShapes = {}
for _,v in ipairs(getElementsByType('vehicle')) do
for i=1, 5 do
if isElementWithinColShape(v, cuboid[i]) then
takenColShapes[i] = v
count = count+1
end
end
end
for i = 1,#takenColShapes do
if takenColShapes[i] then
outputChatBox(i .. " is taken by " .. getVehicleName(takenColShapes[i]))
else
outputChatBox(i .. " is free")
end
end
outputChatBox(count)
```

(Nevermind, this is wrong.)

Edited by Guest

1 is free

1 is taken by Infernus

2 is free

1 is free

and again it spawns car at 1

wtfff

f55 refrreshing

anyone?

Actually, Gr0x his code is alright, with slight modifications.

```taken = {}

for i, v in ipairs(getElementsByType"vehicle") do
for i=1, 5 do
if isElementWithinColShape(v, cuboid[i]) then
taken[i] = v
break
end
end
end

for i=1, 5 do
if taken[i] then
outputChatBox(i.." taken by "..getVehicleName(taken[i]))
else
outputChatBox(i.." is free")
end
end
```

If this doesn't work, give me the full code, I'll test it.

## Create an account

Register a new account