How to check if a player is not in a Region3?

I’m trying to make a “beam” shoot towards a player when they enter a certain region. I have succeeded, but I’m not sure how to remove it when they leave the region. Here is my code:

local pos1 = workspace.Model.RegionBlock.Position - (v.RegionBlock.Size / 2)
local pos2 = workspace.Model.RegionBlock.Position + (v.RegionBlock.Size / 2)
local Region = Region3.new(pos1, pos2)

while wait() do
   local partsInRegion = workspace:FindPartsInRegion3(Region, nil, 1000)
   for _, part in pairs(partsInRegion) do
        -- check if its a player and connect beam.
        -- not sure what to do from here
   end
end

Any help is appreciated. Thank you! :slight_smile:

1 Like

I am still having this issue. :confused:

Perhaps you could make small Region3’s outside of your Region3 which will detect if a player is within them and if they are you would remove the beam?

Try and tell me if it works.

make a list with all the players inside the Region3,constantly loop in the region3 partsInRegion table, and check if the player that was stored in the list earlier is still there, if you can’t find it then it has left.

local players = {}
while wait() do
    for _,part in pairs(partsInRegion) do
        -- get the player blah blah blah
        if table.find(players,player) == nil then 
             --player has left the zone
        end
   end
end
7 Likes

How do you make a list with all the players inside a Region3?

You need to have a table, a region3 and a for loop inside of a while true do loop. You would loop through all the parts inside of that Region3 and check if one of them is the child of a player.

local players = {}
local region = Region3.new(5, 15, 5)
while wait() do
    local partsInRegion = workspace:FindPartsInRegion3(region, nil, 1000)
    for _,part in pairs(partsInRegion) do
      if part.Parent:FindFirstChild("Humanoid") then
          local plr = game.Players:FindFirstChild(part.Parent.Name)
          table.insert(players, plr)
      end
   end
   players = {} -- reset the table after every loop
end
4 Likes

Thank you so much I’ve had this problem for 2 days

1 Like