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

How can I tell when a player has left a Region3?

Asked by
R_alatch 394 Moderation Voter
4 years ago
Edited 4 years ago

I'm just now learning about Region3's because I think it's good for what I'm trying to do. I basically am trying to make a safe zone, where players inside the region will have a ForceField and players outside of it will not. I have found out how to make players inside it have a ForceField, but I'm not sure how to make their ForceField go away when they leave the region. Any help?

local part = script.Parent
local region = Region3.new(part.Position - (part.Size / 2), part.Position + (part.Size / 2))

while true do
    local partsinregion = workspace:FindPartsInRegion3(region, part)

    for _, part in pairs(partsinregion) do
        if part.Parent:FindFirstChild("Humanoid") then
            print("part belongs to player")

            if not part.Parent:FindFirstChild("ForceField") then
                local forcefield = Instance.new("ForceField")
                forcefield.Parent = part.Parent
            end
        end
    end

    wait()
end

Update: I just tried using the TouchEnded function but it did not work sadly!

3 answers

Log in to vote
0
Answered by
St_vnC 330 Moderation Voter
4 years ago

Try checking Yellow Mustang’s video on Region3 : https://m.youtube.com/watch?v=XAwoodFJWos

Ad
Log in to vote
0
Answered by 4 years ago

I have never done this with region3, but i might try to make 3 different table variables,

tableA for containing the parts in region3,

tableB for containing current list of player in region,

tableC for saving the old list of player from the previous iteration of the loop, this changes to be the same as tableB at the end of the loop or when its empty or nil

So how this goes is,

-Detect all the part in region3 put them in tableA using findpartsinregion3 function

-Go through the list of tableA, find player character by finding humanoid, and then find the player with GetPlayerFromCharacter function and put them in the tableB, and give all the characters there the forcefield

-Check if tableC is nil or empty, if nil or empty, just copy tableB and go into the next iteration of the loop

If tableC is not nil or empty, compare tableC and tableB, see which player exist in tableC and not in tableB, those that does not exist on tableB but exist on tableC get their forcefield removed (also checks if they are still in game or if the forcefield still exist).

Im just giving you idea on how this work, how you implement them is up to you.

Also im typing on mobile, excuse grammar and spelling mistakes

Log in to vote
0
Answered by
H34E7R 19
4 years ago

You can try by firstly using the region3 normally and checking if they're in the region3. Inside your if statement for when the player is inside the region3, try including an else statement which means they aren't inside. I haven't had much experience with region3 so I'm not sure if this will help you.

Answer this question