Roblox saves player but gives nil error

is your code inside of a loop?

I don’t understand, can you explain me what are you trying to do more simplified?
Do you want to get all the players in a table?

I am using a lot of loops yea, one loop that loops the whole code over and over and the others break after a certain event is done

I have players that are sitting around a table and I want to put those players in a table

   if plr1 ~= nil then                     
				table.insert(playertable,plr1)      
			
				elseif plr2 ~= nil then
				table.insert(playertable,plr2)
			
				elseif plr3 ~= nil then
				table.insert(playertable,plr3)
			
				elseif plr4 ~= nil then
				table.insert(playertable,plr4)
			
				elseif plr5 ~= nil then
				table.insert(playertable,plr5)
			
				elseif plr6 ~= nil then
				table.insert(playertable,plr6)
			
				elseif plr7 ~= nil then
				table.insert(playertable,plr7)
			
			elseif plr8 ~= nil then
				table.insert(playertable,plr8)
			end

try this just incase

idk if i understood correctly but if u want to store players in a table u can store their names and then search for them later by name so u save strings which is better in my opinion

You can put the seats in a folder and use :GetChildren() to use for loop.

for _Index, Seat in ipairs(Seats:GetChildren()) do
     if Seat:IsA("Seat") and Seat.Occupant ~= nil then
          print(Seat.Occupant.Name)
     end
end

still got the same error

local playersTable = {}

for _Index, Seat in ipairs(Seats:GetChildren()) do
     if Seat:IsA("Seat") and Seat.Occupant ~= nil then
          if Players:FindFirstChild(Seat.Occupant) then
               playersTable[_Index] = Seat.Occupant
          end
     else
          playersTable[_Index] = nil
     end
end

So in this code the empty seats’ index would be nil but the others will be player
like:

table = {
[1] = nil,
[2] = Players.taavZSub,
[3] = Players.This100Times,
[4] = nil
}

1 Like

Do the seats have to be the same name or is this good?
image

to add on to this:





local playersTable = {}
local Players = game:GetService("Players")

for _Index, Seat in ipairs(Seats:GetDescendants()) do
for i, Player in pairs(Players:GetPlayers()) do
     if Seat:IsA("Seat") and Seat.Occupant ~= nil then
          if  Player and Seat.Occupant = Player.Name then
               if not table.find(playersTable, Seat.Occupant) then
               table.insert(playersTable, Seat.Occupant)
             end
          end
       end
     end
end

really appreciate your help tho

The Childern (seats) must be a Seat instance, or you must use GetDescendants(),
so the model will be ignored

Alright, I’ll give it a try. Hope it works

names will not matter since @taavZSub’s does not check for names instead will check for seats

1 Like

OK we got an good information. Occupant property returns the Humanoid not the player. So I will update the code for it.

The code works but do you maybe know how I could re-arrange the table so that the players would be the only thing in the table? For some reason theres also a nil every time in the table making me unable to use the code I already made for it

OK, good news.

I made a place that has 15 seats in the model named Seats in Workspace.

For loop iterates through the model and gets the player to the index of seat so if you sit on the seat 10 then the table will return the below:

{
[10] = "taavZSub"
}

And the updated code is here:

local Players = game:GetService("Players")
local PlayerTable = {}

local SeatsModel = workspace.Seats

while task.wait(2) do
	table.clear(PlayerTable)
	
	for _Index, Value: Seat in ipairs(SeatsModel:GetChildren()) do
		if Value.Occupant then
			local Character = Value.Occupant.Parent
			local Player = Players:GetPlayerFromCharacter(Character)
			
			PlayerTable[_Index] = Player.Name
		else
			PlayerTable[_Index] = nil
		end
	end
	
	print(PlayerTable)
end

But for sure you have to change it to Player instead of Player.Name

Hmm, it seems like the index of seat is broken. So it doesn’t iterate related to seat name, So we will get the index by splitting the name and getting the seat number by the name, cuz the name is Seat5

Ok so doing this gives me an error that the interval is empty when I try to pick a random number between 0 and the tables length. I also tried printing the whole table out as seen above the error and only got nil.

Are you guys still there?
In the updated code:

  • table correctly indexed related to seats number.
    • seat number is declared by the name of the seat: Seat(seat number) for exmpl => Seat5
  • Table values are indicating players instances, so the value will be a player.
  • Empty seats will not be in the table so the table will only include occupied seats.

SeatScript: Script - Seat script in the ServerScriptService

local Players = game:GetService("Players")
local PlayerTable = {}

local SeatsModel = workspace.Seats

while task.wait(2) do
	table.clear(PlayerTable)
	
	for _Index, Value: Seat in pairs(SeatsModel:GetChildren()) do
		if Value:IsA("Seat") and Value.Occupant then
			local Character = Value.Occupant.Parent
			local Player = Players:GetPlayerFromCharacter(Character)
			
			PlayerTable[Value.Name:split("Seat")[2]] = Player
		else
			PlayerTable[Value.Name:split("Seat")[2]] = nil
		end
	end
	
	print(PlayerTable)
end

VARIABLES

Players = Players service
PlayersTable = Table to hold players
SeatsModel = The folder or instance seats are parented to


YOU CAN ALSO FIND THE PLACE BELOW

Classic Baseplate.rbxl (49.3 KB)

1 Like