Table[number] not working

I have an issue, if I try to do table[1], it won’t return that value, and this is critical for the game I’m making. I’ve tried everything I know, but I don’t know why it won’t work. Here’s my defaultdata stuff, basically what _G.sessionData is

local dat = {
	coins = 0;
	isVIP = false;
	isYT = false;
	toolbar = {
	{name = "Rock",lastSwing = 0,durability = math.huge},
	{name = "Wooden Axe",lastSwing = 0,durability = 0};	
	{name = "Wooden Pickaxe",lastSwing = 0,durability = 0};	
	{name = "",lastSwing = 0,durability = 0};	
	{name = "",lastSwing = 0,durability = 0};	
	{name = "",lastSwing = 0,durability = 0};	
	};
	isBanned = false;
	totalRobuxSpent = 0;
	isAdmin = false;
	specialRecipes = {};
	rebirths = 0;
	level = 0;
	xp = 0;
	redeemed = {};
	chests = {};
	inventory = {};
	ownedBoosts = {};
	ownedUpgrades = {};
	ownedAbilities = {};
	ownedSpecialAbilities = {};
	equippedTool = {nil,nil};--tool,slot
	equipped = {
		helmet = nil;
		chestplate = nil;
		leggings = nil;
		accessory = nil;
		hat = nil;
		hair = "Bald";
		face = "Smile";
		shirt = "Default";
		pants = "Default";
		skinColor = "White";
	};
	extraData = {
		antiExploitTriggerNumber = 0;
		equipped = nil;
		lastClan = nil;
		hunger = 100;
		thirst = 100;
		lastSpawn = nil;
		lastJoin = nil;
		antiExploitLogs = {};
		ownedGamepasses = {};
		isDefault = true;
		lastAttacker = nil;
	};
	ownedRebirthItems = {};
	
	
	
	
	
	
	
	
	
}
return

My server code: (keep in mind, this is for a custom inventory system im making)

                                 elseif a == "equipSlot" then
					print("fired")
					-- b = slotNumber
					local c = plr.Character
					local t = c.ToolHold 
					print("checking for sesData")
					print("SLOT WANTED: "..b)
					if not _G.sessionData[plr.UserId] then return end
					local tool
						tool = rep.resources.tools[_G.sessionData[plr.UserId].
						toolbar
						[b]
						.name]
						if not tool then return end
					print("TOOL NAME: ".. _G.sessionData[plr.UserId].toolbar[b].name)
					if not tool then return end
					local ntool = tool:Clone() 
					if not ntool then return end
					print("tool cloned")
					ntool.Parent = c 
					local man = Instance.new("ManualWeld", c.ToolHold) 
					man.Part1 = t 
					man.Part0 = ntool.Reference
					if man and ntool then
						_G.equippedTool = {ntool.Name,b}
					end
				elseif a == "dequipSlot" then
					for _,v in pairs(plr.Character:GetChildren()) do
						if v:IsA("Model") and itemData[v.Name] and itemData[v.Name].category == "tools" then
							v:Destroy()
							_G.sessionData[plr.UserId].equippedTool = {nil,nil}
						end
					end```

And finally, my client code:
```local num = input.KeyCode.Value - 48
		if num >= 1 and num <= 6 then
			--// Check if the slot hit is equipped or not
			local data = mainF:InvokeServer("requestData")
			if data.equippedTool[2] ~= nil or data.equippedTool[2] ~= 0 then
				if data.equippedTool[2] == num then
					-- dequip slot
					functionBank.deselectToolbar()
					mainF:InvokeServer("dequipSlot",num)
				elseif not data.equippedTool[2] == nil or data.equippedTool[2] == 0 then
					 --equip slot
					functionBank.highlightSlot(num)
					mainF:InvokeServer("equipSlot",num)
				else
					-- dequip equipped tool and equip slot
					functionBank.deselectToolbar()
					mainF:InvokeServer("dequipSlot",data.equippedTool[2])
					mainF:InvokeServer("equipSlot",num)
					functionBank.highlightSlot(num)
				end
			else
				functionBank.deselectToolbar()
				mainF:InvokeServer("dequipSlot",data.equippedTool[2])
			end
			
		end
1 Like

And my client code, which may be over the limit:

		if num >= 1 and num <= 6 then
			--// Check if the slot hit is equipped or not
			local data = mainF:InvokeServer("requestData")
			if data.equippedTool[2] ~= nil or data.equippedTool[2] ~= 0 then
				if data.equippedTool[2] == num then
					-- dequip slot
					functionBank.deselectToolbar()
					mainF:InvokeServer("dequipSlot",num)
				elseif not data.equippedTool[2] == nil or data.equippedTool[2] == 0 then
					 --equip slot
					functionBank.highlightSlot(num)
					mainF:InvokeServer("equipSlot",num)
				else
					-- dequip equipped tool and equip slot
					functionBank.deselectToolbar()
					mainF:InvokeServer("dequipSlot",data.equippedTool[2])
					mainF:InvokeServer("equipSlot",num)
					functionBank.highlightSlot(num)
				end
			else
				functionBank.deselectToolbar()
				mainF:InvokeServer("dequipSlot",data.equippedTool[2])
			end
			
		end
1 Like

[number] only applies to arrays. You’ve made a dictionary table, meaning the value in the square brackets has to be the string name of the value in the table.

1 Like

I’m really confused right now, can I get an example?

I think he means like dat.coins or dat[“coins”]

thats what I was using

as right above states .toolbar[b].name)

What table are you trying to access that isn’t working?

I’m trying to access .toolbar, actually.
[b] is a number, and is not nil, but it just wont work, I dont know how to well explain this, but table[number] wont work, the server code is what I sent that is erroring

What @DesiredFlamingFire means is that you cannot get the first item by doing dat[1] since you have a Dictionary.

You will need to index a specific key, example:
dat[“coins”]

1 Like

Is there any error? What does it actually return?

I’ve tried that, or then atleast, not correctly.
I tried dat[“1”] and it isn’t working.

toolbar = {
	["1"] = {name = "Rock",lastSwing = 0,durability = math.huge},
	["2"] = {name = "Wooden Axe",lastSwing = 0,durability = 0};	
	["3"] = {name = "Wooden Pickaxe",lastSwing = 0,durability = 0};	
	["4"] = {name = "",lastSwing = 0,durability = 0};	
	["5"] = {name = "",lastSwing = 0,durability = 0};	
	["6"] = {name = "",lastSwing = 0,durability = 0};	
	};```
How would I fix this and address it properly?

When taking what you provided and printing the name of the rock it worked correctly. What exactly is it outputting and what is the specific code that is the problem?

I just exited studio, it specifies like toolbar[“2”] as nil when it isn’t

Because the table is inside of the dictionary, you need to do dat[“toolbar”][“1”] for it to work.

Thanks, i’ll try that soon. Have a nice day/night

if data.equippedTool[2] ~= nil or data.equippedTool[2] ~= 0 then
this always evaluates to true. you probably meant to write and

The formatting in your post is messed up and I can’t tell what table you’re talking about from your original post because you didn’t say. I don’t know what b is, but if it’s a string and not a number then it won’t get anything from the toolbar table that has only numerical indices.

I tried that, thank you, and now I have once again ANOTHER error.
[20:04:03.679 - ServerScriptService.mainServer: bad argument #2 to ‘?’ (string expected, got nil)]

elseif a == "equipSlot" then
					print("fired")
					-- b = slotNumber
					local c = plr.Character
					local t = c.ToolHold 
					print("checking for sesData")
					print("SLOT WANTED: "..b)
					if not _G.sessionData[plr.UserId] then return end
					local tool
						tool = rep.resources.tools[_G.sessionData[plr.UserId]
						["toolbar"]
						[tostring(b)]
						.name]
						if not tool then return end
					print("TOOL NAME: ".. _G.sessionData[plr.UserId]["toolbar"][tostring(b)].name)
					if not tool then return end
					local ntool = tool:Clone() 
					if not ntool then return end
					print("tool cloned")
					ntool.Parent = c 
					local man = Instance.new("ManualWeld", c.ToolHold) 
					man.Part1 = t 
					man.Part0 = ntool.Reference
					if man and ntool then
						_G.equippedTool = {ntool.Name,b}
					end
				elseif a == "dequipSlot" then
					for _,v in pairs(plr.Character:GetChildren()) do
						if v:IsA("Model") and itemData[v.Name] and itemData[v.Name].category == "tools" then
							v:Destroy()
							_G.sessionData[plr.UserId].equippedTool = {nil,nil}
						end
					end

Defaultdata has the same code as before, i’m really confused now.

It would help if you were specific at all about the line that errors. It doesn’t look like the code you posted even contains the line that the error occurs on.

I really apologize, 316, which in that code would be 12, in the code I just posted

line “12” of that code is [tostring(b)], which is a fragment of five lines that should be one line. That error isn’t caused from this. can you click on the error in the output and scroll down to the place where it says the error is, and also specify what actual function is erroring