Getting a name from a table outside of a function

Im trying to have specific names for a “card” based on a category and number, with the table looking like this:
local cardNames = {
w = {
[1] = “White Card 1”,
[2] = “White Card 2”,
[3] = “White Card 3”
},
s = {
[1] = “Silver Card 1”,
[2] = “Silver Card 2”,
[3] = “Silver Card 3”
},
b = {
[1] = “Black Card 1”,
[2] = “Black Card 2”,
[3] = “Black Card 3”
}
}

however inside of the function further on in the code which provides the category and number like so:
handler.OnServerInvoke = function(player, category, number, invoke)
the code is unable to get the names of the specific cards

print(cardNames) - correctly prints the entire table shown above
print(cardNames[category[number]]) - prints nil

Any help is appreciated!

Have you tried doing print(cardNames.category[number]) instead?

Could you please provide:

1.The client side where you invoke the server from?
2.The entire function [the invoked one]?

this returns the error ServerScriptService.CardHandler:60: attempt to index nil with ‘1’
(when clicking on a card which sends the number 1)

client side is:

local function sendButtonSelection(category, number)
handler:InvokeServer(category, number, 1)
end

-- Button event handlers
s1.MouseButton1Click:Connect(function()
sendButtonSelection("s", "1")
end)

the entire function is:

handler.OnServerInvoke = function(player, category, number, invoke)
	local playerData = playerDecks[player]

	if not playerData then
		playerData = {
			deckamount = 0,
			wdeck = {},
			sdeck = {},
			bdeck = {}
		}
		playerDecks[player] = playerData
	end

	local deckamount = playerData.deckamount
	local wdeck = playerData.wdeck
	local sdeck = playerData.sdeck
	local bdeck = playerData.bdeck

	if invoke == 1 then
		if deckamount < 6 then
			local categoryDeck = nil

			if category == "w" then
				categoryDeck = wdeck
			elseif category == "s" then
				categoryDeck = sdeck
			elseif category == "b" then
				categoryDeck = bdeck
			end

			if categoryDeck then
				print(cardNames)
				print(cardNames.category[number])
				if #categoryDeck < 2 then
					local found = false

					for _, cardName in ipairs(categoryDeck) do
						if cardName == cardNames[category][number] then
							found = true
							break
						end
					end

					if not found then
						local cardName = cardNames[category][number]
						table.insert(categoryDeck, cardName)
						deckamount = deckamount + 1
						playerData[category] = categoryDeck
						playerData.deckamount = deckamount
						print("Added card", cardName, "to", category .. "deck.")
					else
						print("Card already exists in", category .. "deck.")
					end
				else
					print("Category", category, "is already full.")
				end
			else
				print("Invalid category.")
			end
		else
			print("Deck is already full.")
		end
	else
		print("Invalid invoke value.")
	end
end

everything else works as intended, max 6 cards, max 2 per category, previously I had it to just add the number into each category within playerdata which worked fine, but trying to change that to adding the name into the categories

1 Like

Try doing

local cardNames = {
	w = {
		[1] = "White Card 1",
		[2] = "White Card 2",
		[3] = "White Card 3"
	},
	s = {
		[1] = "Silver Card 1",
		[2] = "Silver Card 2",
		[3] = "Silver Card 3"
	},
	b = {
		[1] = "Black Card 1",
		[2] = "Black Card 2",
		[3] = "Black Card 3"
	}
}

for _, tables in pairs(cardNames) do
	for number, card in pairs(tables) do
		print(number, "|", card)
	end
end

Does this work and print every card and its’ corresponding number?

image

Yep

Alright, going off of this;

Maybe you should try to do something like this but instead of printing it you put it into a table and therefore, the code will be able to get the names of the specific cards.
This would probably look like:

local cardNamesTable = {}
for _, tables in pairs(cardNames) do
	for number, card in pairs(tables) do
		table.insert(cardNamesTable, number, card)
	end
end

From here, with this table, you can do:

local function sendButtonSelection(cardNamesTable)
handler:InvokeServer(cardNamesTable, 1)
end

Once it has been sent to the server, you can dissemble the table if you wish or do whatever you need to do with it.

I’m 99% sure the first piece of code I typed in this reply can help you, and 60% sure about the second one. But I’m running on 4.5 hours of sleep right now, so you might wanna proofread the code :sweat_smile: .

1 Like

Found the issue, was a mistake in different part of the scripts using strings vs numbers

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.