Need help with writing multiple answers

local tilesFolder = workspace.Space.Tiles
local tiles = tilesFolder:GetChildren()

for number, tile in tiles do
	while task.wait(1) do
		local OverlapParams = OverlapParams.new()
		OverlapParams.FilterType = Enum.RaycastFilterType.Exclude
		OverlapParams.FilterDescendantsInstances = {}
		OverlapParams.MaxParts = 4
		OverlapParams.CollisionGroup = "Default"

		local boundBox = workspace:GetPartBoundsInBox(CFrame.new(tile.Base.Position.X, tile.Base.Position.Y + 4.5, tile.Base.Position.Z), Vector3.new(6, 6, 6), OverlapParams)
		if boundBox then
			print(#boundBox)
			if #boundBox > 0 then
				print("Tile", (tile.Base.Position.X / 8), tile.Base.Position.Z / 8, "is occupied!")
			else
				print("Tile", (tile.Base.Position.X / 8), tile.Base.Position.Z / 8, "is not occupied!")
			end
		end
	end
end

I have this code, which in idea should get all 25 tiles in a folder, and tell which of them occupied and which are not.

While script mostly works, I can not get the printing part to print all tiles, and instead it gets only 1 tile as an answer. How would I get may be a table or a list of occupied/unoccupied tiles?

1 Like

Add a table. This should work.

local tilesFolder = workspace.Space.Tiles
local tiles = tilesFolder:GetChildren()
local occupied = {}
local vacant = {}
for number, tile in tiles do
	while task.wait(1) do
		local OverlapParams = OverlapParams.new()
		OverlapParams.FilterType = Enum.RaycastFilterType.Exclude
		OverlapParams.FilterDescendantsInstances = {}
		OverlapParams.MaxParts = 4
		OverlapParams.CollisionGroup = "Default"

		local boundBox = workspace:GetPartBoundsInBox(CFrame.new(tile.Base.Position.X, tile.Base.Position.Y + 4.5, tile.Base.Position.Z), Vector3.new(6, 6, 6), OverlapParams)
		if boundBox then
			print(#boundBox)
			if #boundBox > 0 then
                table.insert(occupied,tostring((tile.Base.Position.X) / 8))
			else
                table.insert(vacant,tostring((tile.Base.Position.X) / 8))
			end
		end
	end
end
print(occupied)
print(vacant)

Does it work?

2 Likes

To find a specific tile you can:

local tilePos = PUTPOSITIONHERE
if table.find(occupied,tilePos) then 
   print(table.find(occupied,tilePos))
else
   print(table.find(vacant,tilePos))
end

Or whatever you want.

image
Sadly it seems not to work.