Attempt to index nil with humanoid

My current script is aiming to seat all the players and assign them their roles.

On line 17:
seat:Sit(chosenchar.Humanoid)

It returns the error attempt to index nil with humanoid

Here’s my whole code:

local plrs = game.Players

local survivors = {}

local players = {}

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 = players[playerchosen].Character
			seat:Sit(chosenchar.Humanoid)
			table.remove(players,playerchosen)
		end
	end
	
	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)
			
			plr.PlayerGui.MainRound.RoleShower.Text = "Survivor"
			plr.PlayerGui.MainRound.RoleShower.TextColor3 = Color3.fromRGB(0,255,0)
		end
	end
end)

There have been multiple errors with this, and I’m just really confused.

You’re only getting the number of players, when you should be getting the table of players? :thinking: Change that to:

local playerchosen = math.random(game.Players:GetPlayers())

That now gives this error:
invalid argument #1 to ‘random’ (number expected, got table)

What @Jackscarlett said was right, you’re only getting a number, but he did it wrong.
It’s actually:

local playerchosen = game.Players:GetPlayers()[math.random(#game.Players:GetPlayers())]
-- This gets the player through the random number.

1 is not a valid member of Players “Players”

My guess is cause you only got the player’s name in table.insert(players, v.Name), which could be resulting in that error? Not sure

Edited, I also had a mistake, lol. I tested that this time, it should work (it returned a random player for me).

Still doesn’t work :frowning:

attempt to index nil with 'Character'

Should probably consider printing out the playerchosen variable :thinking: This is quite the pickle

You have another mistake. It’s seat:Sit(playerchosen.Character.Humanoid).
You need to get the player’s character, then the character’s humanoid, otherwise it’ll not work.
Also, yes, I saw you have a chosenchar in there, but really, you don’t need it, you can just do playerchosen.Character.Humanoid

That works! Hooray! But now on the table.remove part I got this error:
invalid argument #2 to ‘remove’ (number expected, got Instance)

Now I have THIS error:
invalid argument #2 to ‘remove’ (number expected, got string)

For the second argument you need to give a number, since the table ‘players’ is an array. You should give a number to remove the value in that position of the table

change this

to this

local playerchosen = math.random(#game.Players:GetPlayers())
local chosenchar = players:GetPlayers()[playerchosen].Character

On chosen char, it now gives this:
attempt to call a nil value

Okay, there was a main mistake with my thinking. I didn’t see that you were completely relying on the players table to check if a player still has to be chosen or not.
You would actually have to use the players table to do all this, if that is what you want to achieve.
But, the way you are saving to the players table is wrong. It is better to save the Player object to the table, so you can easily retrieve the character through that table.

for i,v in pairs(game.Players:GetPlayers()) do
	table.insert(players,v) -- insert player object 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:GetChildren()) -- you can do players:GetChildren() here, as you save the object now - P.S., you can't do :GetPlayers(), as it's not a players service
			local chosenchar = players[playerchosen].Character -- this should work now, since you're saving the player object
			seat:Sit(chosenchar.Humanoid)
			table.remove(players, table.find(players, playerchosen)) -- here, you have to use table.find() to find the player's index
		end
	end

On player chosen:
attempt to call a nil value

maybe this?

local playerchosen = math.random(#players)
local chosenchar = game.Players[players[playerchosen]].Character

On chosenchar I got:
invalid argument #2 (string expected, got Instance)

this would not work since the table ‘players’ is already a table and you are trying to call GetChildren() on a table which does not make any sense

local playerchosen = math.random(#players)

would be fine