How would I detect if there is air under a part

Hello, so what I want to do is make a server script that detects if there is under a part called block and if there is unanchor the part.

It has to be a server script as it would cause to make lag.

If you could help then many thanks.

Is this block on a grid? How many blocks are there? Are they consistent sizes? Raycasting works but depending on how you answer, might not be the best solution.

Ok I got a model called blocks and all its children are called blocks.

What I need is a server script that detects if any there is any part called block and if there is air under it if there is that unanchor that part.

So basically, you want to check if there is a part named “block” under the part right?
Use raycasting. Cast a ray downwards and simply check if there is an instance named “block” intersecting the ray. If yes, then unanchor the part.
Example code:

local part = YOUR_PART
local direction = Vector3.new(0,-5,0) -- five studs downwards
local raycastResult = workspace:Raycast(part.Position, direction) -- starting position, direction
if raycastResult then -- checks if the ray intersected anything
    if raycastResult.Instance.Name == "block" then -- checks if the name is block
        part.Anchored = false -- unanchors
    end
end

Also, Worldroot:Raycast has a 3rd optional parameter called Raycastparams which lets you blacklist some parts, ignore water etc.

Like what @elomala said try cast a ray downwards using raycasting.
And if there is air then unanchor the part.

What I mean is that I need a script in serverscriptservice that detects if there is air under a part called “Block” and if there is air under the part then unanchor the part called “Block”

Do you know how I would do that I am kinda a noob when it comes to scripting.

So “air” under the part means that there isn’t anything under the part?
If so then:

local part = YOUR_PART
local direction = Vector3.new(0,-5,0) -- five studs downwards
-- Also keep in mind if the part is too big you might want to lower -5 to something like -10 or so
local raycastResult = workspace:Raycast(part.Position, direction) -- starting position, direction
if not raycastResult then -- runs the code below if there isn't anything under the part
    part.Anchored = false -- unanchors
end

The code above unanchors the part if there is nothing under the part.
If “air” reffers to something else then let me know

2 Likes

Where would I put the script because I need it in serverscriptservice because if I put it in each block it would cause too much lag.

You can make this a function, and apply it to the parts you need it to be used for.

Can I ask what type of game are you making?

A destruction game why?

Char limit

Following code runs through all of the parts given in a table. You can also just put the parts in a folder and use :GetChildren() instead of specifing each part

You can put it in serverscriptstorage

local parts = {part1, part2, part3} -- table with all of the parts

local direction = Vector3.new(0,-5,0) -- five studs downwards
for i,v in pairs(parts) do
    local raycastResult = workspace:Raycast(v.Position, direction) -- starting position, direction
    if not raycastResult then -- runs the code below if there isn't anything under the part
        v.Anchored = false -- unanchors
    end
end