Hi, I was working with Region 3's to get an array of all the parts within a set area (excluding descendants of the character)
-- from a localscript-- local player = game.Players.LocalPlayer local char = player.Character local region3 = Region3.new( Vector3.new(char.Torso.Position.X -20, char.Torso.Position.Y-5, char.Torso.Position.Z -20), Vector3.new(char.Torso.Position.X+20, char.Torso.Position.Y+10, char.Torso.Position.Z +20) ) local partsInRange = game.Workspace:findPartsInRegion3(region3, char, 20)
This seems to work fine, and when i iterate over the table outputting the names of the parts found in the region3, parts inside the character do not appear.
However, when this line of code runs:
for _, part in pairs(partsInRange) do part.Velocity = char.Torso.CFrame.lookVector * (part.Position - char.Torso.Position).magnitude end
The velocity is added to parts **inside ** the character.
Why is this the case?
Any and all help would be greatly appreciated. Thanks in advanced.
-- EDIT --
A way you can make your code ignore the character is by using the ignoreDescendentsInstance
argument of the FindPartsInRegion3
function. This argument should be an instance, every descendant(everything in it) will be ignored from the returned table but will still count torwards the maxParts argument. So, your code SHOULD work but since for some odd reason it isn't then i'm going to suggest an alternative;
There is no real way to whitelist a Region3
range table effectively without the development of a custom Region3 system.. so an alternative for you is while you're iterating through the partsInRange table, you can check if
the current index's parent is equal to the character. If it isn't then add Velocity
, if it is then don't.
But just doing that won't stop hats or tools from gaining velocity.. so how about we add another conditional to check if
the part's Name is Handle
local player = game.Players.LocalPlayer local char = player.Character local region3 = Region3.new( Vector3.new(char.Torso.Position.X -20, char.Torso.Position.Y-5, char.Torso.Position.Z -20), Vector3.new(char.Torso.Position.X+20, char.Torso.Position.Y+10, char.Torso.Position.Z +20) ) local partsInRange = game.Workspace:findPartsInRegion3(region3, char, 20) for _, part in pairs(partsInRange) do if part.Parent ~= char and part.Name ~= 'Handle' then --Check local dist = (part.Position - char.Torso.Position).magnitude part.Velocity = char.Torso.CFrame.lookVector * dist end end
--EDIT--
Another way you could do this is by using the IsDescendantOf
function for the checking while iterating through the table..
for _, part in pairs(partsInRange) do if not part:IsDescendatOf(char) then local dist = (part.Position - char.Torso.Position).magnitude part.Velocity = char.Torso.CFrame.lookVector * dist end end
--EDIT--
And finally, there's another option.. you could look into the FindPartsInRegion3WithIgnoreList
function. More info here.
Sorry for all the edits, i'm just researching some stuff and editing when I come across something else that could help you solve your problem.
Locked by Goulstem, woodengop, and NinjoOnline
This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.
Why was this question closed?