This is the main script for my round manager. Currently, it seats the player and assigns their roles. But it doesn’t even do that very well.
The problem with my script is the block going from line 40. The error “argument 1 missing or nil” originates on line 44. This thing is a big mess and the are likely many more things wrong with it. Either I’m stupid or it’s just hard.
local players = {}
local playersget = game.Players:GetChildren()
local Randomplayer = nil
local plrs = game.Players
local survivors = {}
function getrandomplayer(chances)
local players = game:GetService('Players'):GetChildren()
local tally = 0
local chances = chances and chances or {} for i,v in pairs(players) do
chances[v] = chances[v] and chances[v] or 2 tally = tally + chances[v]
end
local randnum = math.random(0,tally) tally = 0 for i,v in pairs(players) do
if randnum <= chances[v]+tally then
return v end tally = tally + chances[v]
end
end
game.Players.PlayerAdded:Connect(function()
if #playersget <= 3 then
game.ReplicatedStorage.RoundStatus.WaitingForPlayers:FireAllClients()
end
end)
game.Players.PlayerRemoving:Connect(function()
if #playersget <= 3 then
game.ReplicatedStorage.RoundStatus.WaitingForPlayers:FireAllClients()
end
end)
game.ReplicatedStorage.RoundStart.OnServerEvent:Connect(function()
for i,v in pairs(game.Players:GetPlayers()) do
table.insert(players,v.Name) -- insert player names to table
end
for _,seat in ipairs(workspace.TableArea.Chairs.Seats:GetChildren()) do
if seat:IsA("Seat") and not seat.Occupant then -- if the seat is a seat and doesn't have anyone on it
local playerchosen = math.random(#players, 1)
local chosenchar = workspace:FindFirstChild(players[playerchosen])
seat:Sit(chosenchar.Humanoid)
table.remove(players,playerchosen)
local chosen = plrs:GetChildren()[math.random(1, #plrs:GetChildren())]
chosen.PlayerGui.MainRound.RoleShower.Text = "Murderer"
chosen.PlayerGui.MainRound.RoleShower.TextColor3 = Color3.fromRGB(255, 0, 0)
for i, plr in pairs(plrs:GetChildren()) do
if plr ~= chosen then
table.insert(survivors, plr)
chosen.PlayerGui.MainRound.RoleShower.Text = "Survivor"
chosen.PlayerGui.MainRound.RoleShower.TextColor3 = Color3.fromRGB(0, 255, 0)
end
end
end
end
end)
Remove the 1 from math.random. math.random requires a min and a max number. Min should be the first argument. You can also have 1 argument, which is the max number, so math.random(#players) works too.
local players = {}
local playersget = game.Players:GetChildren()
local Randomplayer = nil
local plrs = game.Players
local survivors = {}
function getrandomplayer(chances)
local players = game:GetService('Players'):GetChildren()
local tally = 0
local chances = chances and chances or {} for i,v in pairs(players) do
chances[v] = chances[v] and chances[v] or 2 tally = tally + chances[v]
end
local randnum = math.random(0,tally) tally = 0 for i,v in pairs(players) do
if randnum <= chances[v]+tally then
return v end tally = tally + chances[v]
end
end
game.Players.PlayerAdded:Connect(function()
if #playersget <= 3 then
game.ReplicatedStorage.RoundStatus.WaitingForPlayers:FireAllClients()
end
end)
game.Players.PlayerRemoving:Connect(function()
if #playersget <= 3 then
game.ReplicatedStorage.RoundStatus.WaitingForPlayers:FireAllClients()
end
end)
game.ReplicatedStorage.RoundStart.OnServerEvent:Connect(function()
for i,v in pairs(game.Players:GetPlayers()) do
table.insert(players,v.Name) -- insert player names to table
end
for _,seat in ipairs(workspace.TableArea.Chairs.Seats:GetChildren()) do
if seat:IsA("Seat") and not seat.Occupant then -- if the seat is a seat and doesn't have anyone on it
local playerchosen = math.random(#game.Players:GetPlayers())
local chosenchar = game.Players:GetPlayers()[playerchosen].Character or game.Players:GetPlayers()[playerchosen].CharacterAdded:Wait()
seat:Sit(chosenchar.Humanoid)
table.remove(players,playerchosen)
local chosen = plrs:GetChildren()[math.random(1, #plrs:GetChildren())]
chosen.PlayerGui.MainRound.RoleShower.Text = "Murderer"
chosen.PlayerGui.MainRound.RoleShower.TextColor3 = Color3.fromRGB(255, 0, 0)
for i, plr in pairs(plrs:GetChildren()) do
if plr ~= chosen then
table.insert(survivors, plr)
chosen.PlayerGui.MainRound.RoleShower.Text = "Survivor"
chosen.PlayerGui.MainRound.RoleShower.TextColor3 = Color3.fromRGB(0, 255, 0)
end
end
end
end
end)