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