luagame-developmentroblox

why is part:GetTouchingParts() returning parts when no parts are touching


I was trying to debug this code

local bird = script.Parent
bird.Anchored = true
local uis = game.UserInputService
local player = game.Players.LocalPlayer
local gravity = -1
local Parts = bird:GetTouchingParts()
local bird_flap = bird.Position + Vector3.new(0, 10, 0)
local script_start = false
uis.InputBegan:Connect(function(input)
    if input.UserInputType == Enum.UserInputType.MouseButton1 then
        script_start = true
    end
    if script_start then
        while not Parts do
            task.wait(0.5)
            gravity = gravity * 2
            uis.InputBegan:Connect(function(input)
                if input.UserInputType == Enum.UserInputType.MouseButton1 then
                    bird.Position = bird_flap
                    gravity = -1
                end
            end)
            bird.Position = bird.Position + Vector3.new(0, gravity, 0)
            task.wait(0.1)
        end
    end
end)

so i tried putting another script in the bird to try to see if something was wrong and tried this code:

local bird = script.Parent
local parts = bird:GetTouchingParts()
if not parts then
    print("no parts")
else
    print("parts touching")
end

it returned with parts touching even though the bird was visibly floating not touching anything


Solution

  • According to the documentation BasePart.GetTouchingParts()

    Returns a table of all parts that are physically interacting with this part.

    So if no parts are touching you'll get an empty table.

    Check the number of table elements instead.