Argument 1 missing or nil

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)

what is exactly line 40 I cant see it?

Main error is:
local chosenchar = workspace:FindFirstChild(players[playerchosen])

Did you try local chosenchar = workspace:FindFirstChild(players.playerchosen)?

Edit: Ahh sorry your argument is nil

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.

1 Like

That now gives this error:
invalid argument #1 to ‘random’ (interval is empty)

Hmmm… try doing math.random(#game.Players:GetPlayers()).

It works! Thanks! But now, I’ve got the argument 1 missing or nil on:
local chosenchar = workspace:FindFirstChild(players[playerchosen])

This is happening because you need to put a string in FindFirstChild. You placed an instance in there so that’s why it is not working. Try doing this:

local chosenchar = workspace:FindFirstChild(players[playerchosen].Name)

You can also do this

local chosenchar = players[playerchosen].Character

“attempt to index nil with Humanoid”
seat:Sit(chosenchar.Humanoid)

This is probably happening because the character is not found (in time). You could try doing a WaitForChild:

local chosenchar = workspace:WaitForChild(players[playerchosen].Name)

That now gives me this error:
“argument 1 missing or nil”

Maybe try this instead:

local chosenchar = players[playerchosen].Character or players[playerchosen].CharacterAdded:Wait()

If that’s not working, then I’m really confused.

Now I have THIS error:
attempt to index nil with ‘Wait’

Oh, wait I just realized we made some changes for choosing a random player before.

This should work lol

local chosenchar = game.Players:GetPlayers()[playerchosen].Character or game.Players:GetPlayers()[playerchosen].CharacterAdded:Wait()

Now when I force start the round it doesn’t. Oof

But it did find the character and seated it?

Nope, nothing in the console, nothing happened to the player

Could you resend the script here again, so I can see the changes?

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)