Table always returning nil whenever I try to receive information

So I have table that appears to look like this after the function is ran:

playerData["mrfrogg1"] = {
	
	 Slot1 = {Name = "Mag-G18", Type = "Mag", BulletType = "9mm", Left = "15", Max = "15"},  Slot2 = {},  Slot3 = {},  Slot4 = {},  Slot5 = {},  Slot6 = {},  Slot7 = {},  Slot8 = {},  Slot9 = {}, Slot10 = {}, Slot11 = {}, Slot12 = {}, Slot13 = {}, Slot14 = {}, Slot15 = {}, Slot16 = {}, 
	Slot17 = {}, Slot18 = {}, Slot19 = {}, Slot20 = {}, Slot21 = {}, Slot22 = {}, Slot23 = {}, Slot24 = {}, Slot25 = {}, Slot26 = {}, Slot27 = {}, Slot28 = {}, Slot29 = {}, Slot30 = {}, Slot31 = {}, Slot32 = {},
	Slot33 = {}, Slot34 = {}, Slot35 = {}, Slot36 = {}, Slot37 = {}, Slot38 = {}, Slot39 = {}, Slot40 = {}, Slot41 = {}, Slot42 = {}, Slot43 = {}, Slot44 = {}, Slot45 = {}, Slot46 = {}, Slot47 = {}, Slot48 = {},
	Slot49 = {}, Slot50 = {}, Slot51 = {}, Slot52 = {}, Slot53 = {}, Slot54 = {}, Slot55 = {}, Slot56 = {}, Slot57 = {}, Slot58 = {}, Slot59 = {}, Slot60 = {}, Slot61 = {}, Slot62 = {}, Slot63 = {}, Slot64 = {},
	
	locationX = 0,
	locationY = 0,
	locationZ = 0,
	
	health = 100,
	food = 100,
	water = 100,
	lifeTime = 0,
	bleeding = false;
	
	bagEquipped = "None",
	primaryEquipped = {},
	secondaryEquiped = {},
	hatEquipped = {},
	faceEquipped = {},
	shirtEquipped = {},
	pantsEquipped = {}
}

And whenever I try to do the following:

local slotName = "Slot" .. slot
local playerName = "mrfrogg1"
local selectedTablePLSWork =  playerData[playerName][slotName]
print(selectedTablePLSWork["Name"])

The system would output nil.
Just to clarify, printing:

print(selectedTablePLSWork)

would output the table key, so I am assuming I am doing something wrong in regards to getting the table variable “Name”.

Edit:

Proof that the data is stored

[{“Slot27”:[],“bleeding”:false,“Slot54”:[],“Slot34”:[],“health”:100,“Slot45”:[],“Slot20”:[],“Slot49”:[],“secondaryEquiped”:[],“Slot55”:[],“Slot21”:[],“water”:100,“Slot58”:[],“Slot12”:[],“Slot53”:[],“Slot43”:[],“Slot22”:[],“Slot26”:[],“Slot17”:[],“Slot6”:[],“pantsEquipped”:[],“Slot15”:[],“Slot39”:[],“Slot14”:[],“Slot33”:[],“Slot37”:[],“Slot23”:[],“bagEquipped”:“None”,“Slot32”:[],“Slot3”:[],“Slot38”:[],“Slot31”:[],“Slot56”:[],“Slot8”:[],“Slot48”:[],“Slot60”:[],“Slot9”:[],“Slot4”:[],“Slot57”:[],“food”:100,“Slot25”:[],“Slot44”:[],“Slot11”:[],“Slot52”:[],“Slot13”:[],“Slot7”:[],“Slot5”:[],“Slot10”:[],“Slot46”:[],“Slot61”:[],“Slot59”:[],“faceEquipped”:[],“Slot16”:[],“Slot47”:[],“shirtEquipped”:[],“Slot36”:[],“Slot63”:[],“Slot18”:[],“hatEquipped”:[],“Slot1”:[{“BulletType”:“9mm”,“Left”:4,“Max”:15,“Name”:“Mag-G18”,“Type”:“Mag”}],“Slot24”:[],“Slot42”:[],“Slot2”:[],“Slot64”:[],“Slot19”:[],“locationY”:0,“locationZ”:0,“Slot51”:[],“lifeTime”:0,“Slot50”:[],“Slot28”:[],“Slot29”:[],“Slot62”:[],“primaryEquipped”:[],“Slot30”:[],“Slot35”:[],“Slot40”:[],“locationX”:0,“Slot41”:[]}]

Edit2:

Function used to assign the data:
table.insert(playerData[player.Name][slotName], {
				Name = object.Name, 
				Type = object.Type.Value, 
				BulletType = object.BulletType.Value,
				Left = object.Left.Value,
				Max = object.Max.Value
			})

So it sounds like you can get the slot table fine, but you can’t get the name.

I suggest printing stuff so you can have a better understanding of what the code is looking at:

  1. print the slot name to know which slot you’re looking at
  2. use a for loop to print all elements in the table (key and value) to get an idea of what’s in your slot table

I’m assuming you’re indexing the wrong slot. Out of 64 slots, only 1 has a “Name” value.

1 Like

Im really sorry for being a bit vague on much of the information:

Screenshot (757)

If Im being a bit confusing:

My table as soon as game executes, which is assigned to me:

local defaultTable = {
	
	 Slot1 = {},  Slot2 = {},  Slot3 = {},  Slot4 = {},  Slot5 = {},  Slot6 = {},  Slot7 = {},  Slot8 = {},  Slot9 = {}, Slot10 = {}, Slot11 = {}, Slot12 = {}, Slot13 = {}, Slot14 = {}, Slot15 = {}, Slot16 = {}, 
	Slot17 = {}, Slot18 = {}, Slot19 = {}, Slot20 = {}, Slot21 = {}, Slot22 = {}, Slot23 = {}, Slot24 = {}, Slot25 = {}, Slot26 = {}, Slot27 = {}, Slot28 = {}, Slot29 = {}, Slot30 = {}, Slot31 = {}, Slot32 = {},
	Slot33 = {}, Slot34 = {}, Slot35 = {}, Slot36 = {}, Slot37 = {}, Slot38 = {}, Slot39 = {}, Slot40 = {}, Slot41 = {}, Slot42 = {}, Slot43 = {}, Slot44 = {}, Slot45 = {}, Slot46 = {}, Slot47 = {}, Slot48 = {},
	Slot49 = {}, Slot50 = {}, Slot51 = {}, Slot52 = {}, Slot53 = {}, Slot54 = {}, Slot55 = {}, Slot56 = {}, Slot57 = {}, Slot58 = {}, Slot59 = {}, Slot60 = {}, Slot61 = {}, Slot62 = {}, Slot63 = {}, Slot64 = {},
	
	locationX = 0,
	locationY = 0,
	locationZ = 0,
	
	health = 100,
	food = 100,
	water = 100,
	lifeTime = 0,
	bleeding = false;
	
	bagEquipped = "None",
	primaryEquipped = {},
	secondaryEquiped = {},
	hatEquipped = {},
	faceEquipped = {},
	shirtEquipped = {},
	pantsEquipped = {}
}

I then run this function which would recieve the mag, telling it me, the slot to be put in, and the mag instance:

local function addSlotItem(player, slot, object)
	
	if object.Type ~= nil then -- This ensures reliability
		
		if object.Type.Value == "Gun" then
		
		elseif object.Type.Value == "Mag" then
			print("ReachedMag")
			
			local slotName = "Slot" .. slot
			table.insert(playerData[player.Name][slotName], {
				Name = object.Name, 
				Type = object.Type.Value, 
				BulletType = object.BulletType.Value,
				Left = object.Left.Value,
				Max = object.Max.Value
			})
			--playerData[player.Name][slot]
			
			
			print(object.Name .. " picked up successfully to slot " .. slot)
			
			local selectedTablePLSWork =  playerData[player.Name][slotName]
			print(selectedTablePLSWork["Name"])
			object:Destroy()
		end
		
	end
	
end

Screenshot (758)

The table after (jsoned)

 [{"Slot27":[],"bleeding":false,"Slot54":[],"Slot34":[],"health":100,"Slot45":[],"Slot20":[],"Slot49":[],"secondaryEquiped":[],"Slot55":[],"Slot21":[],"water":100,"Slot58":[],"Slot12":[],"Slot53":[],"Slot43":[],"Slot22":[],"Slot26":[],"Slot17":[],"Slot6":[],"pantsEquipped":[],"Slot15":[],"Slot39":[],"Slot14":[],"Slot33":[],"Slot37":[],"Slot23":[],"bagEquipped":"None","Slot32":[],"Slot3":[],"Slot38":[],"Slot31":[],"Slot56":[],"Slot8":[],"Slot48":[],"Slot60":[],"Slot9":[],"Slot4":[],"Slot57":[],"food":100,"Slot25":[],"Slot44":[],"Slot11":[],"Slot52":[],"Slot13":[],"Slot7":[],"Slot5":[],"Slot10":[],"Slot46":[],"Slot61":[],"Slot59":[],"faceEquipped":[],"Slot16":[],"Slot47":[],"shirtEquipped":[],"Slot36":[],"Slot63":[],"Slot18":[],"hatEquipped":[],"Slot1":[{"BulletType":"9mm","Left":12,"Max":15,"Name":"Mag-G18","Type":"Mag"}],"Slot24":[],"Slot42":[],"Slot2":[],"Slot64":[],"Slot19":[],"locationY":0,"locationZ":0,"Slot51":[],"lifeTime":0,"Slot50":[],"Slot28":[],"Slot29":[],"Slot62":[],"primaryEquipped":[],"Slot30":[],"Slot35":[],"Slot40":[],"locationX":0,"Slot41":[]}](rbxopenscript://www.dummy.com/dummy?scriptGuid=%7BAFFD6F2E%2DB02F%2D4E6A%2D919A%2D90D5A70186A2%7D&gst=3#255) 

(unjsoned):

playerData["mrfrogg1"] = {
	
	 Slot1 = {Name = "Mag-G18", Type = "Mag", BulletType = "9mm", Left = "15", Max = "15"},  Slot2 = {},  Slot3 = {},  Slot4 = {},  Slot5 = {},  Slot6 = {},  Slot7 = {},  Slot8 = {},  Slot9 = {}, Slot10 = {}, Slot11 = {}, Slot12 = {}, Slot13 = {}, Slot14 = {}, Slot15 = {}, Slot16 = {}, 
	Slot17 = {}, Slot18 = {}, Slot19 = {}, Slot20 = {}, Slot21 = {}, Slot22 = {}, Slot23 = {}, Slot24 = {}, Slot25 = {}, Slot26 = {}, Slot27 = {}, Slot28 = {}, Slot29 = {}, Slot30 = {}, Slot31 = {}, Slot32 = {},
	Slot33 = {}, Slot34 = {}, Slot35 = {}, Slot36 = {}, Slot37 = {}, Slot38 = {}, Slot39 = {}, Slot40 = {}, Slot41 = {}, Slot42 = {}, Slot43 = {}, Slot44 = {}, Slot45 = {}, Slot46 = {}, Slot47 = {}, Slot48 = {},
	Slot49 = {}, Slot50 = {}, Slot51 = {}, Slot52 = {}, Slot53 = {}, Slot54 = {}, Slot55 = {}, Slot56 = {}, Slot57 = {}, Slot58 = {}, Slot59 = {}, Slot60 = {}, Slot61 = {}, Slot62 = {}, Slot63 = {}, Slot64 = {},
	
	locationX = 0,
	locationY = 0,
	locationZ = 0,
	
	health = 100,
	food = 100,
	water = 100,
	lifeTime = 0,
	bleeding = false;
	
	bagEquipped = "None",
	primaryEquipped = {},
	secondaryEquiped = {},
	hatEquipped = {},
	faceEquipped = {},
	shirtEquipped = {},
	pantsEquipped = {}
}
1 Like

I have found my own problem, but thank you @RuizuKun_Dev and @Eqicness for the help/assistance though :slight_smile:
Problem: table.insert used incorrectly

table.insert(playerData[player.Name][slotName], {
				Name = object.Name, 
				Type = object.Type.Value, 
				BulletType = object.BulletType.Value,
				Left = object.Left.Value,
				Max = object.Max.Value
			})

Solution:

playerData[player.Name][slotName]["Name"] = object.Name
playerData[player.Name][slotName]["Type"] = object.Type.Value
playerData[player.Name][slotName]["BulletType"] = object.BulletType.Value
playerData[player.Name][slotName]["Left"] = object.Left.Value
playerData[player.Name][slotName]["Max"] = object.Max.Value

Not going to lie, this really is a facepalm moment for me. Primarily because of how Ive set up the previous items it would make logical sense for it to continue its pattern on the third table XD