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 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
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
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