I currently have 2 functions, one to check when a player has left a region and one for if a new player joins the region. I'm having issues with getting it to remove the player from the table if they have left the region.
local regionPart = workspace:WaitForChild("RegionPart")
local region = Region3.new(regionPart.Position - (regionPart.Size / 2), regionPart.Position + (regionPart.Size / 2))
local playersInRegion = {}
local function addToRegion()
for _, part in ipairs(partsInRegion) do
if part.Parent:FindFirstChild("Humanoid") then
local player = game:GetService("Players"):GetPlayerFromCharacter(part.Parent)
table.insert(playersInRegion, player)
if not player.Character:FindFirstChild("ForceField") then
local forceField = Instance.new("ForceField", player.Character)
end
end
end
end
local function removeFromRegion()
for partIndex, part in ipairs(partsInRegion) do
if part.Parent:FindFirstChild("Humanoid") then
local player = game:GetService("Players"):GetPlayerFromCharacter(part.Parent)
if not table.find(playersInRegion, player) then
table.remove(playersInRegion, player)
player.Character:FindFirstChild("ForceField"):Destroy()
end
end
end
end
while wait() do
partsInRegion = workspace:FindPartsInRegion3(region)
addToRegion()
removeFromRegion()
end

1 comment
Hmm.. ill see what i can come up with, You mind if i re-write the code?