This is a ServerScript inside ServerScriptService. When I test the game alone, it works fine. But when I test with two accounts, sometimes it selects one account as the killer (meaning the chosen player works) but gives the killer skin to the other player (meaning the char bit does not work properly).
I have a high suspicion it’s because of something to do with the type of loop like it already starting and defining everything before the other player joins.
When I change:
while wait(5) do
to:
while true do
It prints this error:
ServerScriptService.RolePickerScript:5: invalid argument #2 to ‘random’ (interval is empty)
while wait(5) do
local plrs = game.Players
local survivors = {}
local chosen = plrs:GetChildren()[math.random(1, #plrs:GetChildren())]
game.Workspace.NoobName.Value = chosen.Name
local char = game.Workspace:FindFirstChild(chosen.Name)
print(char) -- this prints correctly
local chosen = plrs:GetChildren()[math.random(1, #plrs:GetChildren())]
chosen.PlayerGui.Picker.Background.RoleGiven.Text = "Noob"
chosen.PlayerGui.Picker.Background.RoleGiven.TextColor3 = Color3.fromRGB(0, 170, 255)
chosen.PlayerGui.Picker.Background.Visible = true
noobify()
armKiller() -- these functions work just not for the right player, they are both
-- set to char which is the character of the chosen player.
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(255, 255, 255)
plr.PlayerGui.Picker.Background.Visible = true
end
end
game.ReplicatedStorage.PickNoob:FireAllClients(chosen)
end