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

How can I assign random roles to random players?

I have tried to do it in the following script, but it isn't really random, as I just take random players. What is the best way to assign random roles to the players?

local Players = game:GetService("Players")
local Roles = game.ReplicatedStorage.roles:GetChildren()
local Playerstable = game.Players:GetPlayers()

Villagers = {}
Werewolves ={}
Arsonists = {}
Hunters ={}
Mediums ={}
Seers ={}
Jesters ={}
Witches ={}

function Randomizeplayer()
local Randomplayer = Players:GetChildren()[math.random(1,#Players:GetChildren())]
print(Randomplayer)
if table.getn(Werewolves) < 3
then
	table.insert(Werewolves,1,Randomplayer)
	print(table.unpack(Werewolves))
    print(table.unpack(Villagers))
else
	table.insert(Villagers,1,Randomplayer)
	print(table.unpack(Werewolves))
    print(table.unpack(Villagers))
end
end


game.Players.PlayerAdded:Connect(Randomizeplayer)

Thanks in advance!

4 comments

0

Is "table.getn(Werewolves)" trying to get the number of current werewolves?

0

yup

0

its raw random, add weights

0

add weights?

1 answer

I've never used getn before, so I would recommend using the # to get the number items on a list. You are also attempting to give a role to a random person whenever someone joins, and what I think you wanted to do was give a role to the player that has just joined. To do that, playerAdded gives you the Player that has joined, so we can rewrite the script to look like this.

function assignRole(player) --Player is given to use by playeradded
	if #Werewolves < 3 then
	    table.insert(Werewolves,1, player)
	    print(table.unpack(Werewolves))
	    print(table.unpack(Villagers))
	else
	    table.insert(Villagers,1, player)
	    print(table.unpack(Werewolves))
	    print(table.unpack(Villagers))
	end
end

game.Players.PlayerAdded:Connect(assignRole)

I made a little adjustment and if it is not what you wanted, leave a comment.

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