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

How to select players in a rotation?

Asked by 5 years ago

Currently my method is straight up math.random and that leads to multiple same player selects and that's seems to unfair to me.

1local canid = {}
2    for i,v in pairs(game.ReplicatedStorage.PlayerInfo:GetChildren()) do
3        table.insert(canid, v.Name)
4        v.PersonType.Value = "Survivor"
5    end
6    local hunter = canid[math.random(#canid)]
7    game.ReplicatedStorage.PlayerInfo.PlayerHolder:FindFirstChild(hunter).PersonType.Value = "Hunter"

What I'm looking for is a way to select all players in a rotation. That way everyone has a chance to play as the Hunter. Is there any way to make that in lua?

0
Add the players to a table once they are selected ForeverBrown 356 — 5y
0
My script is on an constant loop so I'm not sure how that would help? JexaButterscotch 73 — 5y

1 answer

Log in to vote
2
Answered by 5 years ago
Edited 5 years ago
1local alreadySelected = {}

-- I wrote a function for you to check if the player was already selected:

1function check(player)
2    for i,v in pairs(alreadySelected) do
3        if v == player then
4            return true
5        end
6    end
7end

-- now when selecting a hunter you will do

1if check(plr.Name) then
2    -- rechoose because player was already selected this rotation
3else
4    table.insert(alreadySelected, plr.Name)
5    -- make player hunter
6end

now to know and reset the table when everyone has been a hunter you can just do

1if #Players == #alreadySelected then
2    alreadySelected = {} -- this sets the table empty again
3    -- start rolling hunter again
4end

You'll also want to have a playerLeaving function and use table.remove() to remove the player from the table if he is in it, if you don't do this then if #Players == #alreadySelected then will bug out and your table will never reset! If this helped, please mark this as the answer

Ad

Answer this question