Need help on my Rolepicking system script isn't working

How can you help me fix this problem i’m having?

local Weapon = game.ServerStorage.Knife

local plrs = game.Players

local survivors = {}

 
while wait(10) do

local chosen = plrs:GetChildren() 
[math.random(1, #plrs:GetChildren())]
local Character = chosen.Character

chosen.StarterGui.Picker.Back.Role.Text ="Murderer"

chosen.StarterGui.Picker.Back.Role.TextColor3 = Color3.fromRGB(255, 0, 0)

               
chosen.StarterGui.Picker.Back.Visible = true

killerWeapon:Clone().Parent = chosen.Backpack

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

   if plr ~= chosen then

       table.insert(survivors, plr)

plr.StarterGui.Picker.Back.Role.Text = "Survivor"
plr.StarterGui.Picker.Back.Role.TextColor3 = 
Color3.fromRGB(0, 255, 0)

 plr.StarterGui.Picker.Back.Visible = true

       end

end

end

2 Likes

Could you go into more depth on the issue that you’re having?

1 Like

Let me provide a video for you

1 Like

What do I change up so it won’t be index

1 Like


As you see both players have Murderer and non get a sword, only one should say Murderer and get a sword and the other says survivor.

But he isn’t indexing a table, he put # before it, which returns the length of a table.

Do any of you scripters know why this is not working why one is not becoming a survivor?

More in depth on the issue I’m having the issue I’m having is one player would become the Murderer but when I run the game on test on 2 players they both become the Murderer one is a pose to become the Murderer and the other a survivor, what part in the script is wrong did I miss type a part in the script.

To pick a player randomly, use that code:

local picked
function pickPlayer()

	local canBePicked = {}
	for _, player in ipairs(game.Players:GetPlayers()) do
		table.insert(canBePicked, #canBePicked+1, player)
	end
	
	if #canBePicked > 0 then -- if there is more than one player who can be picked
		picked = canBePicked[math.random(1, #canBePicked)]
	end
	return picked -- returns the player picked
end
local playerPicked = pickPlayer()

Try this

if plr.Name ~= chosen.Name then
1 Like

Thanks I’ma try this, also How can this code work for the Gui which I do have made, would say that that player is the murderer?

Would that fix the problem i’m having?

Yeah try that on your original code.

Ok, thanks for the help I’ma try it out.

It depends on how you organised your frames but I can give you an example:

local picked
function pickPlayer()

	picked = nil
	local canBePicked = {}
	
	for _, player in ipairs(game.Players:GetPlayers()) do
		table.insert(canBePicked, #canBePicked+1, player)
	end
	
	if #canBePicked > 0 then
		picked = canBePicked[math.random(1, #canBePicked)]
	end
	return picked
end

local playerPicked = pickPlayer()

local frame = script.Parent.Frame
frame.PlayerName.Text = playerPicked.Name..' is the murderer!!'

In addition to this, I recommand you to pick randomly a player throughout the server and not the client. To do this, use a RemoteEvent to transfer either the Client > Server, or Server > Client. Here’s some documentations you can refer to: Custom Events and Callbacks | Documentation - Roblox Creator Hub

1 Like

You can easily pick a player by using the following

local players = game.Player:GetChildren()
local picked = players[math.random(1, #players)]

You are right, but the math.random doesn’t work if the player is alone in the game and would cause an error

No? math.random works inclusively meaning that it would pick a random number between 1 and 1 which would be 1.

1 Like

Here is some sample code that would give you the functionality you are looking for.

local players = game.Players:GetChildren()

local murderer = players[math.random(#players)]
--Fire a ClientEvent to tell the Client that he or she is the murderer

for _, survivor in pairs(players) do
    if (survivor == murderer) then
        continue
    end

    --Fire a ClientEvent to tell the Client that he or she is a survivor
end

EDIT: There would also need to be some sanity checks to make sure there are more than two players in the server because this code would choose the murderer but there would be no survivors meaning the rest of the code would not function properly.

2 Likes

Oops, you are right I thought it would give an error ahah. My bad!

1 Like