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

How to have a script randomly select players until all players have been chosen?

Asked by 8 years ago

Basically I am running a script where it will randomly select two players who will compete against each other. Once those two players are chosen, a boolean value inside their player will be made true, showing they've already played. On the next competition, how would I make sure that the players who already competed do not compete again?

game.Players.PlayerAdded:connect(function(plr) local a = Instance.new("BoolValue",plr)
    a.Name = "HasGoneThisRound"
    a.Value = false
end)

1 answer

Log in to vote
1
Answered by
1waffle1 2908 Trusted Badge of Merit Moderation Voter Community Moderator
8 years ago

You don't need to create a new object for every player and physically store it inside of the game. You can do all of this in the script.

local competed={}
function ChooseTwo()
    local available={}
    for _,v in pairs(game.Players:GetPlayers())do
        if not competed[v]then
            available[#available+1]=v
        end
    end
    if #available<2 then return end
    local first=table.remove(available,math.random(#available))
    local second=available[math.random(#available)]
    competed[first]=true
    competed[second]=true
    return first,second
end

Store all of the players who have competed in a table, list all of the players who are not in that table, choose two randomly. The function will return nothing if there are not two players left to choose from, otherwise it returns the two players and adds them to the table.

0
Thank you very much. Ethan_Waike 156 — 8y
Ad

Answer this question