Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

[SOLVED] question :FindPartsInRegion3()?

Asked by 6 days ago
Edited 4 days ago

This is my question; is there a way that workspace:FindPartsInRegion3() can also find parts not parented to workspace, for example: parts or models in ServerStorage or ReplicatedStorage that have Vector3Positions?

0
what do you mean "use it with a table of parts", it is a function of workspace theking48989987 785 — 6d
0
?????????????????????????????????????????? DO NOT UNDERSTAND DominusInfinitus 135 — 6d
0
U simply cant. EXpodo1234ALT 1321 — 6d

2 answers

Log in to vote
0
Answered by 4 days ago

You can achieve your desired effect with a generic loop and magnitude.

-- if there are parts in ReplicatedStorage for example:
local rsFile = game:GetService("ReplicatedStorage"):GetDescendants()
local gather = function()
local partTable = {}
        for i,object in pairs(rsFile) do
        if (object:IsA("Part")) then table.insert(partTable,object)
    end
end
    return partTable
end

local loop = function()
local root = workspace:WaitForChild(game.Players.LocalPlayer.Name).HumanoidRootPart
            for i,part in pairs(gather()) do
-- here     local distance = (root.Position - part.Position).magnitude
            if (distance < 100) then print(distance.." range lost")
            elseif (distance > 100) then print(distance.." range found")
        end
    end
end

while true do -- finally, I used a while loop to continually return the distance
    wait(1)
    loop()
end 

Practice created loops and when you can; especially with large tables, use numeric loops it's somewhat faster.

0
This is what I needed, thanks! darkhenry 20 — 4d
Ad
Log in to vote
0
Answered by
incapaxian 1315 Badge of Merit Moderation Voter
6 days ago

I do not understand your question. But :FindPartsInRegion3() returns an array of parts that are in the Region3 you specify. I will show you an example of :FindPartsInRegion3()

--[[
    """
    If you want to create a Region3 from a part,
    you can use this formula
    Region3.new(part.Position - (part.Size/2), part.Position + (part.Size/2))
    """
--]]
local part = game.Workspace.Part -- # some part
local region = Region3.new(part.Position - (part.Size/2), part.Position + (part.Size/2))
local partsInRegion = game.Workspace:FindPartsInRegion3(
    region, -- # Region to work with
    nil, -- # Instance to ignore
    math.huge -- # Maximum part count (defaults to 20)
)

for _, partInRegion in ipairs(partsInRegion) do
    print(partInRegion)
end

Answer this question