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
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
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.
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.