Check if air is below a part, HOW?

Wassup gamers!

I have a really simple question that people already know the answer to, How can I check if air is below a part?

I’m working on a beyblades thing and he wants it so the blade can’t go on the walls. (The beyblade goes to the players mouse location.)

Anyways, thanks in advance!!! : DDD

1 Like

I don’t really understand your question, do you want it to see if it’s not standing on anything with a material?

1 Like

Okso, I just want to see if there’s a part (or nothing) below my mouse position. : )

You can probably shoot a ray down from the position and see if it hits something
There is a nice tutorial here on raycasting

1 Like

Fire a ray with the origin as part.Position and the direction as -part.CFrame.UpVector * ((part.Size.Y / 2) + 1). This will make a ray going down from the part to 1 stud past the part.

You can then use the result from that ray and get the material and check if it is equal to Enum.Material.Air.

edit: oops i accidentally said remote instead of ray lmao

1 Like

alr! ill try everything here! Thank you guys!

So I’m looking at this, And I’m not sure how to integrate it to the player’s mouse… any ideas? lol.

are you looking for the 3D position of the part that the mouse is pointing at?

Yes I am! I’m just not sure how to detect if a part is under the mouse position.

ok instead of part.Position, try using

local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()

-- origin for the ray: mouse.Hit.Position
-- direction: Vector3.new(0, -1, 0)

Here’s what I have, idk why, but it isn’t working.

local rayOrigin = mouse.Hit.Position
	local rayDirection = Vector3.new(0,-1,0)
	local raycastResult = workspace:Raycast(rayOrigin, rayDirection)
	
	if raycastResult ~= Enum.Material.Air then

(Thank you for helping me with this, I’m not too good with Raycasting lol.)

You shold have raycastResult and raycastResult.Material ~= Enum.Material.Air then on the last line, because the material is a field of the raycast result

1 Like

What about this?

1 Like

Something weird, now its not moving unless you’re not touching the ground xD

I’ll look at this too, tysm! :smiley:

Wait, It’s semi working! Thank you! I’ll mess around with this to get it working!

I am writing a function for you, please wait.

Alr! I’ll be back and check it in 20-30 minutes, thank you!

Here’s a function I wrote for you (the thing is the part has to be tiny on X and Z axis):

local part = script.Parent
local listedInstances = {game:GetService("Workspace"):WaitForChild("Baseplate")}

local function IsTouchingOnlyListedInstances(touchingParts)
    local onlyListed = true
    for i, v in pairs(touchingParts) do
        if not table.find(listedInstances, v) then
            onlyListed = false
        end
    end
    return onlyListed
end

local airIsBelowThePart
if #part:GetTouchingParts() == 0 then
    airIsBelowThePart = true
    print("Part is not touching anything.")
    print("We can say air is below part.")
elseif IsTouchingOnlyListedInstances(part:GetTouchingParts()) then
    airIsBelowThePart = true
    print("Part is only touching listed instances.")
    print("We can say air is below part.")
else
    airIsBelowThePart = false
    print("Part is touching something that is not listed.")
end

if airIsBelowThePart then
    print("Air is below the part.")
else
    print("Air is not below the part.")
end

You can add instances to the table while runtime.

For example the part touches few invisible blocks. You list them in the table and the function will return that air is below the part.

Note that this is not very reliable.

I found out a solution. Here it is:

local part = script.Parent
local rayDirection = Vector3.new(0, - 0.8, 0)
local raycastResult = game:GetService("Workspace"):Raycast(part.Position, rayDirection)

if raycastResult then
    print("Air is not below.")
else
    print("Air is below.")
end

Your code should look like this:

 local rayOrigin = mouse.Hit.Position
 local rayDirection = Vector3.new(0, - 0.8, 0)
 local raycastResult = game:GetService("Workspace"):Raycast(rayOrigin, rayDirection)

 if raycastResult then
     print("Air is not below.")
 else
     print("Air is below.")
 end

It wasn’t working @AP00F, @TheBigC10 because raycastResult will be nil if there isn’t any parts below the part.

1 Like