# isTakingSpace function not working

I wrote this function

``````function CreateRegion3FromLocAndSize(Position, Size)
local SizeOffset = Size/2
local Point1 = Position - SizeOffset
local Point2 = Position + SizeOffset
return Region3.new(Point1, Point2)
end

function isTakingSpace(pos,part)
local whitelist = {part}
local region3 = CreateRegion3FromLocAndSize(pos,Vector3.new(1,1,1))
local parts = workspace:FindPartsInRegion3WithWhiteList(region3,whitelist,10)
--print(unpack(parts))
for i,v in pairs(parts) do
if v==part then
return true
end
end
return false
end
``````

## This is the idea:

You pass in a Vector3 position and a MeshPart/Union
It returns true if the position is being obstructed in a 1x1 space by the MeshPart/Union
Otherwise, it returns false

## This is the problem:

For whatever reason, it always returns true

# I’m stumped

1 Like

Before i preceded to try and find a solution, what is the isTakingSpace function supposed to do?

As stated in the original post

It’s basically supposed to check if a specific Vector3 position is inside the specified part or not.

A 2D example of this would be as follows

A would return true
B would return false

Alright so i tested this and it seemed to work fine for me, are you sure that when testing your function the position wasn’t the position of the part?

Also as an alternative to using region’s using this function might be better:
Edit:(Cleaned it up a bit)

``````local function IsWithinObject(Object, Position)
local Offset = 0
local min, max = Object.Position.X - (Object.Size.X + Offset)/2, Object.Position.X + (Object.Size.X + Offset)/2
local minZ, maxZ = Object.Position.Z - (Object.Size.Z + Offset)/2 , Object.Position.Z +(Object.Size.Z + Offset)/2
local minY, maxY = Object.Position.Y - (Object.Size.Y + Offset)/2, Object.Position.Y +(Object.Size.Y + Offset)/2

if math.clamp(Position.X, min,  max) ~= (min and max )and  math.clamp(Position.Z, minZ,  maxZ) ~=(minZ and maxZ) and  math.clamp(Position.Y, minY,  maxY) ~=(minY and maxY)  then

return true
else

return false
end
end``````
1 Like

I’m afraid that function doesn’t work with MeshParts.
It also seems that my function doesn’t work with MeshParts either.

The bounds seem to remain rectangular.
If you pass the function this blue block’s position and then MeshPart for example, it still returns true.

I’m going to try using GetTouchingParts instead and see if that makes a difference.

I see what you mean, didn’t think of that hmm… GetTouchingParts Should work…

1 Like

Ok looks like this works:

``````local function GetTouchingParts(part)
local connection = part.Touched:Connect(function() end)
local results = part:GetTouchingParts()
connection:Disconnect()
return results
end

function isTakingSpace(pos,part)
local checkPart = Instance.new('Part',part.Parent)
checkPart.Size = Vector3.new(.1,.1,.1)
checkPart.Anchored = true
checkPart.CFrame = CFrame.new(pos)
local touching = GetTouchingParts(checkPart)
for i,v in pairs(touching) do
if v == part then
checkPart:Destroy()
return true
end
end
checkPart:Destroy()
return false
end
``````

Also, if you’re wondering why I’m using a seemingly redundant function to GetTouchingParts, check out this post:

1 Like

If you’re wondering what this is for, it’s for this:

1 Like