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

Why is my repeat loop not ending? (Answered)

Asked by
Unhumanly 152
4 years ago
Edited 4 years ago

So I'm trying to randomize teams in my game.

When players first join the game before the round starts, their team is defaulted to Neutral, of which the TeamColor is White. And yes, I created a team instance, named it "Neutral", and changed its TeamColor to White.

When I begin the round, I want to determine how many players will go on the two other teams, Blue and Red. I do this by taking the number of players in the game, and dividing it by 2. Of course, I have to account for odd numbers. So I set up two variables.

local PlayersforBlue = math.ceil(game.Players.NumPlayers/2)
local PlayersforRed = math.floor(game.Players.NumPlayers/2)

Note that the round will only start when there are at least 2 players in the game

I even wrote a simple command into the command bar to check to see if these two variables account for every player in the game, for every possible player count. The bool I printed was to determine whether or not it did account for every player. The output read "true" every single time.

for i = 2, 12 do 
    local PlayersforBlue = math.ceil(i/2)
    local PlayersforRed = math.floor(i/2) 
    print("Players: " .. i, "Blue: " .. PlayersforBlue, "Red: " .. PlayersforRed) 
    if PlayersforBlue + PlayersforRed == i  then 
        print(true) 
    else 
        print(false) 
    end 
end

Now having confirmed that these two variables do in fact account for every player, it was time to put them to use.

for i = 1, PlayersforBlue do
    repeat 
        wait()
        local ran = math.random(1, game.Players.NumPlayers)
        plr = game.Players:GetPlayers()[ran]
        return plr
    until plr.TeamColor == BrickColor.new('White')
    plr.TeamColor = BrickColor.new('Bright blue')
end

Now this is where the problem is. The repeat loop doesn't end, regardless of the fact that all of the players in the game the first time it runs are teamed to Neutral, of which the TeamColor is White.

I double-checked all the TeamColors. The team named "Blue" has a TeamColor of Bright blue. The team named "Red" has a TeamColor of Bright red. The team named "Neutral" has a TeamColor of White.

I legitimately do not know what I did wrong, and can't spot my error.

I put print statements everywhere, and it stops at the "until" line, because the script is indicating that the condition has not been met, regardless of the fact that it has been.

Can someone please point my mistake out?

0
Neutral's color isn't exactly white. You have the wrong "white" color. DeceptiveCaster 3761 — 4y
0
No, I literally created three teams, and named one of them "Neutral", and made its TeamColor White. Unhumanly 152 — 4y
0
You're returning player before letting 'until' end your loop. B_rnz 171 — 4y
0
Thank you! Didn't know. Gotta learn somehow. Unhumanly 152 — 4y

Answer this question