Keep getting chosen murder

I was following a tutorial on how to make a role picker. After I was finished it worked good but when I tested it with my friend, he got chosen Killer and stayed killer for the entire server until someone else joined.

local killerWeapon = game.ServerStorage.ClassicSword

local plrs = game.Players

local survivors = {}


while wait(20) do
		
	local chosen = plrs:GetChildren()[math.random(1, #plrs:GetChildren())]	
			
	chosen.PlayerGui.Picker.Background.RoleGiven.Text = "Murderer"
	chosen.PlayerGui.Picker.Background.RoleGiven.TextColor3 = Color3.fromRGB(255, 0, 0)
					
	chosen.PlayerGui.Picker.Background.Visible = true
	
	killerWeapon:Clone().Parent = chosen.Backpack
	
			
	for i, plr in pairs(plrs:GetChildren()) do
		
		if plr ~= chosen then
			
			table.insert(survivors, plr)

			plr.PlayerGui.Picker.Background.RoleGiven.Text = "Survivor"
			plr.PlayerGui.Picker.Background.RoleGiven.TextColor3 = Color3.fromRGB(0, 255, 0)
			
			plr.PlayerGui.Picker.Background.Visible = true
			
		end	
	end	
end

How do I fix this problem?

2 Likes

Try #plrs:GetPlayers() instead of GetChildren

Try using Random.new():NextInteger() instead.

local killerWeapon = game.ServerStorage.ClassicSword

local plrs = game.Players

local survivors = {}


while wait(20) do
	local chosen = plrs:GetPlayers()[Random.new():NextInteger(1, #plrs:GetPlayers())]

	chosen.PlayerGui.Picker.Background.RoleGiven.Text = "Murderer"
	chosen.PlayerGui.Picker.Background.RoleGiven.TextColor3 = Color3.fromRGB(255, 0, 0)

	chosen.PlayerGui.Picker.Background.Visible = true

	killerWeapon:Clone().Parent = chosen.Backpack


	for i, plr in pairs(plrs:GetChildren()) do

		if plr ~= chosen then

			table.insert(survivors, plr)

			plr.PlayerGui.Picker.Background.RoleGiven.Text = "Survivor"
			plr.PlayerGui.Picker.Background.RoleGiven.TextColor3 = Color3.fromRGB(0, 255, 0)

			plr.PlayerGui.Picker.Background.Visible = true

		end	
	end	
end

Tried that but the whole thing broke

1 Like

Could you print out what “chosen” returns?

I tested out this code and it works perfectly fine.

local plrs = game.Players

while wait(2) do
	local chosen = plrs:GetPlayers()[Random.new():NextInteger(1, #plrs:GetPlayers())]
	warn(chosen.Name)
	warn(chosen.ClassName)
end

Hmm thats interesting. I’ll try it. I’ll update you if it doesn’t work

Hey don’t you think this will stop the entire script because it will keep running in a loop