How can I detect if there is a part inside of a viewmodel? (PLACEMENT SYSTEM)

  1. What do you want to achieve? Keep it simple and clear!
    I want to make a localscript that detects if there is a part inside of the viewmodel (the viewmodel is kinda like the placing part before you have placed it). I don’t want it to detect if a part is touching it, only if a part is a little bit inside of it.

  2. What is the issue? Include screenshots / videos if possible!
    I don’t know much about this.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have tried YT and Devforum

Find the object that represents your viewmodel (e.g., a tool or a model) and insert a LocalScript into it.
Here’s an example:

local viewmodel = script.Parent -- Change 'script.Parent' to the actual path of your viewmodel object
local partToCheck = workspace.PartToCheck -- Change 'workspace.PartToCheck' to the actual path of the part you want to check

-- Function to check if a part is partially inside the viewmodel
local function isPartInside(part)
    local viewmodelRegion = viewmodel:GetBoundingBox()
    local partRegion = part:GetBoundingBox()

    -- Check if the part's region intersects with the viewmodel's region
    return viewmodelRegion:IntersectsWith(partRegion)
end

-- Connect the function to a checking event (e.g., using a loop or a user input event)
-- For example, you can check this condition when a user clicks a button or presses a key
-- Replace 'MouseButton1Click' with the actual event you want to trigger this check
viewmodel.MouseButton1Click:Connect(function()
    if isPartInside(partToCheck) then
        print("The part is partially inside the viewmodel.")
    else
        print("The part is not inside the viewmodel.")
    end
end)

i got an error when i changed it to my script: GetBoundingBox is not a valid member of UnionOperation “Workspace.Foundation”

Also, how do i check to see if any part is inside of the viewmodel not just a specific part

Ok, I think I fixed the error, and I changed the script so it checks if any part is inside the viewmodel by checking each part’s vertices. Let me know if it works.

local viewmodel = script.Parent -- Change 'script.Parent' to the actual path of your viewmodel object

-- Function to check if a point is inside the viewmodel
local function isPointInside(point, model)
    local localPoint = model:WorldToLocal(point)
    local size = model.Size / 2
    return math.abs(localPoint.x) < size.x and
           math.abs(localPoint.y) < size.y and
           math.abs(localPoint.z) < size.z
end

-- Connect the function to a checking event (e.g., using a loop or a user input event)
-- For example, you can check this condition when a user clicks a button or presses a key
-- Replace 'MouseButton1Click' with the actual event you want to trigger this check
viewmodel.MouseButton1Click:Connect(function()
    local workspaceParts = workspace:GetDescendants() -- Get all parts in the workspace

    for _, part in pairs(workspaceParts) do
        if part:IsA("BasePart") then -- Check if the object is a part
            local partVertices = part:GetVertices()

            for _, vertex in pairs(partVertices) do
                local worldPosition = part.CFrame:PointToWorldSpace(vertex)
                if isPointInside(worldPosition, viewmodel) then
                    print("A part is partially inside the viewmodel.")
                    return
                end
            end
        end
    end

    print("No part is inside the viewmodel.")
end)

1 Like

This is good and all, but it checks to see if theres a basepart in it. How can I make it so that if anything at all is in it then it will go red not just from baseparts.

This script will consider all objects in the workspace, not just BasePart objects, when checking for objects inside the viewmodel.

local viewmodel = script.Parent -- Change 'script.Parent' to the actual path of your viewmodel object

-- Function to check if an object is inside the viewmodel
local function isObjectInside(object, model)
    local objectPosition = object.Position
    local modelPosition = model.Position
    local modelSize = model.Size / 2

    local relativePosition = model:WorldToLocal(objectPosition)

    return math.abs(relativePosition.x) < modelSize.x and
           math.abs(relativePosition.y) < modelSize.y and
           math.abs(relativePosition.z) < modelSize.z
end

-- Connect the function to a checking event (e.g., using a loop or a user input event)
-- For example, you can check this condition when a user clicks a button or presses a key
-- Replace 'MouseButton1Click' with the actual event you want to trigger this check
viewmodel.MouseButton1Click:Connect(function()
    local workspaceObjects = workspace:GetDescendants() -- Get all objects in the workspace

    for _, object in pairs(workspaceObjects) do
        if isObjectInside(object, viewmodel) then
            print("An object is inside the viewmodel.")
            return
        end
    end

    print("No object is inside the viewmodel.")
end)
1 Like

But how come it says, if Object:IsA(“BasePart”)

1 Like

a BasePart is a superset of Part, MeshPart, Wedge, etc. (or anything that can be rendered). afaik theres no use of including non-BaseParts

what is this? theres no such thing as :GetVertices()??? are you using chatgpt :skull:

1 Like

Hmmm, Seems like a reasonable assumption. GPT can output a response with that line of code when you generate a prompt that finds collisions in LUA.