Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to select players in a rotation?

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?

2 comments

0

Add the players to a table once they are selected

0

My script is on an constant loop so I'm not sure how that would help?

1 answer

✓ Accepted answer
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

This discussion is preserved as it was. New answers, comments, and votes are closed.