I've been trying to make a script to change player's team when player go out of regoin for my prison, I tested it and it worked but when player exit the region while there more than one player in region, the player's team wont change here my script

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

while wait() do
	wait()
	for i,player in pairs(game.Players:GetChildren()) do
		local character = player.Character.Head
		if table.find(CharacterWhiteList, character) == nil then
			table.insert(CharacterWhiteList, character)
		end
	end
	
	local parts = game.Workspace:FindPartsInRegion3WithWhiteList(region, CharacterWhiteList) -- only find player's body part for no lag
	for i,part in pairs(parts) do
		local player = game.Players:GetPlayerFromCharacter(part.Parent)
		if player then
			table.insert(PlayerInRegion, player)
		end
	end
	
	for i,player in pairs(game.Players:GetChildren()) do
		if table.find (PlayerInRegion, player) == nil then
			if player.Team == game.Teams.Prisoner then
				player.Team = game.Teams.Criminal
			end
		end
	end
	table.clear(PlayerInRegion)
	table.clear(CharacterWhiteList)
end

2 comments

0

Are there any errors in the output? I have an idea why this happens though I must confirm it.

0

There no error in the output when I tested, even with 2 players.