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

Why does my change team when player exit region only work when there one person in region?

Asked by 3 years ago
Edited 3 years ago

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
0
Are there any errors in the output? I have an idea why this happens though I must confirm it. imKirda 4491 — 3y
0
There no error in the output when I tested, even with 2 players. TossyToss 0 — 2y

Answer this question