Why is my table not reading the correct number of players

I am attempting to make a round system but for some reason the code is not waiting until the number of players in the table is 1. The print statement before even prints out 2 so i dont know why its not waiting. I get no errors.

local public = {}

local ProfileController = require(script.Parent.Data.ProfileController)
local Calculator = require(script.Parent.Data.Calculator)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game.Players
local Leveling = require(script.Parent.Data.Leveling)

local Arena = workspace.Arena

local Events = ReplicatedStorage.Remotes.Events
local Functions = ReplicatedStorage.Remotes.Functions

local ARENA_INTERVAL = 15
local InArena = false

local PlayersInArena = {}

Events.Arena.JoinArena.OnServerEvent:Connect(function(player)
    table.insert(PlayersInArena, player.Name)
end)

Events.Arena.DamagePlayer.OnServerEvent:Connect(function(originPlayer, targetPlayerName)
    local Calculator = Calculator:getCalculator(originPlayer)
    
    local targetPlayerCharacter = workspace[targetPlayerName]
    local humanoid = targetPlayerCharacter.Humanoid

    local damageOutput = Calculator:calculateDamageOutput()

    humanoid:TakeDamage(damageOutput)
end)

local function PlayerWon(player)
    local PlayerLeveling = Leveling:getLevelManager(player)
    local Calculator = Calculator:getCalculator(player)
    local PlayerController = ProfileController:getProfileManager(player)
    local gemAmount = Calculator:calculateGemArenaAmount()
    local xpAmount = Calculator:calculateArenaLevelAward()
    Events.Arena.PlayerWon:FireAllClients(player.Name, gemAmount, xpAmount)
    PlayerLeveling:increaseLevel(xpAmount)
    PlayerController:incrementStat("Gems", gemAmount, "PlayerStats")
    PlayerController:incrementStat("ArenaWins", 1, "PlayerStats")
end

while true do
    game:GetService("RunService").Stepped:Wait()
    if not InArena and #game.Players:GetPlayers() >= 1 then
        wait(ARENA_INTERVAL)
        Events.Arena.RequestArena:FireAllClients()

        InArena = true
        
        for i = 10, 1, -1 do
            wait(1)
            ReplicatedStorage.ArenaCountdown.Value = "Teleporting In "..i
        end

        wait(1.5)

        for _,v in pairs(PlayersInArena) do
            if Players:FindFirstChild(v) then
                local character = game.Players[v].Character
                local randX = math.random(Arena.Corner2.Position.X, Arena.Corner1.Position.X)
                local randZ = math.random(Arena.Corner3.Position.Z, Arena.Corner2.Position.Z)
                character.HumanoidRootPart.CFrame = CFrame.new(randX, -127, randZ)
                character.Humanoid.WalkSpeed = 0
                character.Humanoid.JumpPower = 0
                Players[v].gamestats.ChickenState.Value = 2
            end
        end

        wait(1)

        for _,v in pairs(PlayersInArena) do
            if Players:FindFirstChild(v) then
                Events.Arena.StartArena:FireClient(Players[v])
            end
        end

        wait(6)

        for _,v in pairs(PlayersInArena) do
            if Players:FindFirstChild(v) then
                local character = Players[v].Character
                character.Humanoid.WalkSpeed = 16
                character.Humanoid.JumpPower = 50
            end
        end

        wait(3)

        print(#PlayersInArena)

        repeat wait() until #PlayersInArena == 1 or 0

        if #PlayersInArena == 1 then
            local player = Players:FindFirstChild(PlayersInArena[1])

            if player then
                PlayerWon(player)
            end
        end

        for _,v in pairs(PlayersInArena) do
            if Players:FindFirstChild(v) then
                local character = Players[v].Character
                character.HumanoidRootPart.CFrame = workspace.Spawns:GetChildren()[math.random(1, #workspace.Spawns:GetChildren())].CFrame + Vector3.new(0, 5, 0)
                Players[v].gamestats.ChickenState.Value = 1
            end
        end


        InArena = false

        PlayersInArena = {}
    end
end 

return public

This is a lot of code to just throw out here, use a judgement call on what’s actually important; it would also be helpful to mark the section of code that’s being problematic.

I’m assuming this is the line: repeat wait() until #PlayersInArena == 1 or 0
This conditional can be written as: (#PlayerInArena == 1) or 0
The outcomes of this statement are true or 0 and false or 0, which give true and 0 respectively.
Both are truthy values, the loop terminates immediately.

The solution is: #PlayerInArena <= 1

1 Like