Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I check if nothing is inside of a region3?

I'm trying to make a script that detects if a Region is empty, though nothing happens when I use an if statment to check if there is nothing in the region. Here is what I got;

  local region = Region3.new(Vector3.new(1,1,1),Vector3.new(10,10,10))

local event = game.ReplicatedStorage:WaitForChild("test")

event.OnServerEvent:Connect(function(player)

local regionParts = workspace:FindPartsInRegion3(region,nil,100)

for i, parts in pairs(regionParts) do

if parts == nil then

print("yes")

else

print("erg")

end

end

end)

I don't know what I'm doing wrong, any help would be much appreciated!

3 comments

0

That's not all of the code. Where's the part where you define the region?

0

Right, sorry. I forgot to put them here.

0

You're attempting to run a for loop on an array if zero elements. If there is nothing in the region, the loop won't run even once and the if check becomes superfluous. Since the function returns an array, you can simply use the length operator (#) to the the number of parts found. Try it. Delete the for loop and just do print(#regionParts)

1 answer

✓ Accepted answer

If the table is empty, it won't be traversed.

Simply check if the array length is equal to zero:

if (rawequal(#regionParts, 0)) then
	--// do something
end

1 comment

0

Ah, I see. Thank you.

This discussion is preserved as it was. New answers, comments, and votes are closed.