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

Choosing a random role from a table?

Im making an amoung game and I need help getting the script to choose players roles (Imposters, crewmate, ect)

Here are the important parts:

local roles = {"Imposter","Innocent"}

for i = 5,0,-1 do
	TopBar.Text = "Round starting in "..i.." seconds!"
	print(i)
	
	if i < 1 then
		game.Players:GetChildren().Character.CFrame = Vector3.new(game.Workspace.RoundSpawn)
		print("Round has started!")
		break
	end
end

After it breaks out of the loop, I want the roles to be chosen, can someone help?

1 answer

✓ Accepted answer

You can pick a random item from the table by using math.random, you'd also need to iterate through all the players and assign each player to the role that was chosen using math.random, then use an if statement to check if they're an imposter or crewmate.

local roles = {"Imposter", "Innocent"}

for i = 5,0,-1 do
	
	TopBar.Text = "Round starting in "..i.." seconds!"
	print(i)
	wait(1)
	
	if i < 1 then
		
		for _, player in pairs(game.Players:GetPlayers()) do

			if player then
				local Character = player.Character or player.CharacterAdded:Wait()
				
				local role = roles[math.random(#roles)]	
				
				print(player.Name.." has been assigned to "..role)

				if role == "Imposter" then
					-- Code
				end

				Character.PrimaryPart.CFrame = (workspace.RoundSpawn.CFrame)
			end
		end
	end
end

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