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

How do you stop the character being included in region3's? [closed]

Asked by
lama321 10
9 years ago

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.

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?

1 answer

Log in to vote
1
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
9 years ago

-- 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.

0
thanks Goulstem, I'll be sure to try that lama321 10 — 9y
1
but if there is no way to whitelist a region 3 table then what doeswhat does ignore descendants actually do lama321 10 — 9y
0
It does ignore descendants. But the argument has to be used as an instance - meaning that all descendants of that instance would be ignored, and this isn't always going to be the case that you want. (That moment when goulstem realizes he was wrong). Lol let me edit my answer(: Goulstem 8144 — 9y
0
Hm.. this is tough. The ignoreDescendants argument SHOULD work. But since it doesn't(I just tested it in studio as well) then I guess you have to use my suggested alternative. Goulstem 8144 — 9y
Ad