Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to find models that are touching part?

Asked by 1 year ago

Hello, I am trying to make a script which finds a number of how many models are touching a part. The model is NPC and it has same name as other NPCS there is ussualy alot of them. Here is a script that dosen't really work it gives me only an amount of parts touching but not how many models touching.

local part = script.Parent

local TopLeftFrontOfPart = part.Position - (0.5 * part.Size)
local TopRightBackOfPart = part.Position + (0.5 * part.Size)

local region = Region3.new(TopLeftFrontOfPart,TopRightBackOfPart)
local count = 0
script.Parent.Touched:Connect(function()
    for _,Part in pairs(game.Workspace:FindPartsInRegion3(region,nil,math.huge)) do
        if Part.Parent.Name == "Dummy" then
            count = count + 1

        end
    end
    print(count)
    count = 0
end)

Thanks.

3 answers

Log in to vote
0
Answered by
MattVSNNL 620 Moderation Voter
1 year ago

Try this

if Part.Parent:IsA("Model") then

    local model = Part.Parent

end
0
It wouldn't work because it would still do it with every part of model kristupas12344 26 — 1y
Ad
Log in to vote
0
Answered by
bdam3000 125
1 year ago

Maybe try this. Basically, I just made a table and inserted models into it.

local part = script.Parent

local TopLeftFrontOfPart = part.Position - (0.5 * part.Size)
local TopRightBackOfPart = part.Position + (0.5 * part.Size)

local region = Region3.new(TopLeftFrontOfPart,TopRightBackOfPart)
local count = 0

local models = {}

script.Parent.Touched:Connect(function()
    for _,Part in pairs(game.Workspace:FindPartsInRegion3(region,nil,math.huge)) do
        if Part.Parent:IsA('Model') then
            if models[Part.Parent] then
                return
                else
                    table.insert(models, Part.Parent)
            end
        end
    end
    count = (#models)
    print(count)
    table.clear(models)
    count = 0
end)

Log in to vote
0
Answered by
enes223 327 Moderation Voter
1 year ago

hey you! have you ever heard of enes? if you are in trouble, better call enes!

Answer this question