I’ve created a range part that follows the player character (The range part is under the HumanoidRootPart position). I’m trying to make it so It’d detect all the parts inside the Range Part.
An example here would be that the range part detects coins
You can use the GetTouchingParts
method of the range part’s BasePart
object.
I’ve already try that but it doesn’t seem it detects the objects as the Range Part CanCollide is set to false.
Ah! Obviously it won’t if it is set to false as it relies on collision detection to determine which parts are touching the range part.
Since it’s set to false I’d recommend you use the FindPartsInRegion3
method to detect parts within a specified region, which can be the bounding box of the range part.
I’ve never used FindPartsInRegion3. Could you give me an example code as I don’t know how it works?
Yass! Here’s a code I found on the forum. x
-- Define the range part
local rangePart = workspace.RangePart
-- Create a region to search for parts
local region = Region3.new(rangePart.Position - rangePart.Size / 2, rangePart.Position + rangePart.Size / 2)
-- Function to detect parts inside the range part
local function detectParts()
local parts = workspace:FindPartsInRegion3(region)
for _, part in pairs(parts) do
if part:IsDescendantOf(workspace) then
print(part.Name .. " is inside the range part!")
end
end
end
-- Call the detectParts function repeatedly
while true do
detectParts()
wait() -- wait a short time before checking again
end
I’ll try that and let you know if it works!
for i,v in pairs(game.Players:GetPlayers()) do
if (v.Character.HumanoidRootPart.Position - coin.Position).Magnitude <= range then
--task
end
end
Something like this?
Thanks for your help! It worked.
I’ve a question though, The range part only detects if the part is in the center. How could I make it so it’d be in any of the range part place?
You can modify the detectParts
function to check if the part’s bounding box intersects with the region of the range part.
Can you give a small example code of how it would work?
Yasss of course!
-- Function to detect parts inside the range part
local function detectParts()
local parts = workspace:GetDescendants()
for _, part in pairs(parts) do
if part:IsA("BasePart") and part:IsDescendantOf(workspace) then
local partRegion = part:GetBoundingBox()
if region:IntersectsWith(partRegion) then
print(part.Name .. " is inside the range part!")
end
end
end
end
The BoundingBox part throws me an error in the output:
Here is my code:
local region = Region3.new(glass.Position - glass.Size /2 , glass.Position + glass.Size/2)
local parts = workspace:GetDescendants()
for _, part in ipairs(parts) do
if part.Parent == workspace.Map.GrassLand.Coins then
local partRegion = part:GetBoundingBox()
if region:IntersectsWith(partRegion) then
part:Destroy()
end
end
end
Can you test my script? Im pretty sure its what you want
My script uses a range part but you use magnitude which won’t work…
Just loop over GetPartsInPart
FindPartsInRegion3 is deprecated
local params = OverlapParams.new()
-- Only include Coins
params.FilterDescendantsInstances = workspace.Map.GrassLand.Coins
params.FilterType = Enum.RaycastFilterType.Include
for _, hit in workspace:GetPartsInPart(rangePart, params) do
print("Touching a coin:", hit)
end
GetPartsInPart() only works if collision was on.
It would work if CanCollide on.
The only reason it might not work is if you turn off CanQuery.
GetPartsInPart() is different from GetTouchingParts(), GetTouchingParts() is older and shouldn’t be used, GetPartsInPart() is the replacement, which will also get parts with CanCollide false.
Since your part seems to be a sphere, you could also use GetPartsInRadius() which would probably be more performant for you, that way you don’t need any parts.