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.

local canid = {}
    for i,v in pairs(game.ReplicatedStorage.PlayerInfo:GetChildren()) do
        table.insert(canid, v.Name)
        v.PersonType.Value = "Survivor"
    end
    local hunter = canid[math.random(#canid)]
    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
local alreadySelected = {}

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

function check(player)
    for i,v in pairs(alreadySelected) do
        if v == player then
            return true
        end
    end
end

-- now when selecting a hunter you will do

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

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

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

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