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
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.
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()
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
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.