How to pick a random player and assign a role to him

I am embarrassed that I don’t know this but well it is what it is basically

how do I randomly select a player and then assign any role to it? much as like the game murder mystery it takes a player and assign a murder / sheriff role to it

I have made a script but it dosen’t work when there’s no players in the session i tried putting a PlayerAdded function but the problem is that I only want to select one player and when I have inserted the PlayerAdded it ran the math.random function everytime a player joins and thats a massive problem

but here is the script anyways


	local playerTable = game.Players:GetPlayers()

	local randomPlayer = playerTable[math.random(1, #playerTable)] -- selects the random player

	print(randomPlayer) -- prints the selected player name

and by the way its a script not a local script and it is in the serverscriptservice

If the script you’ve given works, this is how you do it

Make a script, in which it picks the random player(already done here). Then, try getting the players humanoid. Then, add an attribute such as Murderer, Sherrif to the humanoid, and so forth, and add a true or false value

This is how you add an attribute

1 Like

good, I will try this method thanks for the help

you need to make an intermission system and periodically start the round, here’s the pseudocode:

-- server script
local waitTime = 15
local minPlayer = 2

while task.wait(waitTime) do
  if #players < minPlayer then continue end

  -- select random player
  -- give the role
end
2 Likes

For some reason the intermission script you made dosent work idk why?

Using this should help

local randomplr = {}
for i, v in pairs(game.Players:GetPlayers())
table.insert(randomplr, v)
end

then just get a random person from table

1 Like

It’s because Sussy used “return” in the while loop, which does not really give the desired effect.

Here is something simple that you could use:

local RequiredPlayers = 2
while true do
	-- Intermission
	local CurrentPlayers = 0
	repeat
		task.wait(1)
		CurrentPlayers = #game.Players:GetPlayers()
	until CurrentPlayers >= RequiredPlayers 
	-- Wait before teleporting
	task.wait(1)
	-- Teleport (just a example)
end
4 Likes

THANK YOU I’ve been trying to figure out that exact script for hours and yet haven’t found the clear solution for it until you came appreciate it and appreciate all the other people in this thread who have tried to help me

1 Like

hey i got a small problem here


local PlayersInSession = game.Players:GetChildren() -- players
	
local players = game.Players 


local RequiredPlayers = 1

while true do
	
	wait(5)	-- Intermission
	
	local CurrentPlayers = 0
	
	repeat
		print("Not enough players in session to star the round")
		task.wait(1)
		
		CurrentPlayers = #game.Players:GetPlayers()
		
	until CurrentPlayers >= RequiredPlayers 
	
	
	print("selecting seeker")
	
	task.wait(1)


	local RandomPlayer = PlayersInSession[math.random(1,#PlayersInSession)]
		

	print(RandomPlayer) -- picked player
	

end

my problem is that this error pops up when the randomplayer runs

ServerScriptService.GameplayScripts.Script:19: invalid argument #2 to ‘random’ (interval is empty)

from what i understood the problem is that the random player variable runs before the players spawn in the game and i dont know how to delay it maybe iam just wrong but i dont know

never mind a simple solution but i dont think it will be affecting in the actual game would be that i just put a wait(15) until alll players spawn in thats it

can you conclude the part where it randomly select a player?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.