Needing help making a table within another table

I’m creating some form of “guess the word” script so that players can click a letter and make a word and I need to store each word they get correct in a table so I can reward them if they complete it.

I’m struggling to store these values and getting the error attempt to index field '?' (a nil value)
I’ve tried experimenting and doing different things to get it to work but I was suspect I was going to have this issue before I began as I’ve never really known how to properly do it.

I’d like to know how to store each letter as its own form of randomized numbers so a word can have more than one letter. I’m just not too sure where to begin on implenting this

Main table > Player > Stored Letters

Code:

local Letter = "A"
local PlayersInActivation = {}

for _, clickLetter in next, workspace.Event.Main.CaveLetters:GetChildren() do
	clickLetter.ClickDetector.MouseClick:Connect(function(Player)
		print(0)
		local isComplete = EventComplete:GetAsync(CompleteKey)
		if isComplete ~= true then
			print(1)
			
			local letter = clickLetter.Name
			
			if not PlayersInActivation[Player] then
				table.insert(PlayersInActivation, Player)
				
				PlayersInActivation[Player]["letters"] = {} -- where the error is
				print(2)
			end
			
			for i = 0, string.len(Letter), 1 do
				print(3)
				if letter == string.sub(Letter, i) then
					for _, myLetters in next, PlayersInActivation[Player]["letters"] do
						print(4)
						if not myLetters == letter then
							print(5)
							table.insert(PlayersInActivation[Player]["letters"], letter)
						end
					end
				end
			end
			
			
			if #PlayersInActivation[Player]["letters"] == string.len(Letter) then
				print(6)
				local succ, err = pcall(function()
					EventComplete:SetAsync(CompleteKey, true)
				end)
				
				if succ then
					print(Player.Name.. " saved easter")
					script.Disabled = true
				end
			end
		end
	end)
end

Any help will be appreciated, thank you for taking the time to read this.

3 Likes

The function table.insert() adds a new value to the table under the next numerical key. Meanwhile you’re trying to use the table like a dictionary table by doing PlayersInActivation[Player].

What you should do instead is set the value like this, without using table.insert:

PlayersInActivation[Player] = {letters = {}}

Although I don’t see a need to have so many tables inside one another.

1 Like

Usually what I do with 2-dimensional tables is use custom set and get methods for it, and create a new table in the first index if it doesn’t exist.
Aka:

local tabl = {}

function tabl:set(index1,index2,value)
    if self[index1] then
        self[index1][index2] = value
    else
        self[index1] = {[index2] = value}
    end
end

function tabl:get(index1,index2)
    if self[index1] then
        return self[index1][index2]
    else
        return nil
    end
end
2 Likes

Thank you, just one last question.
How would I index the letters into a for loop?

I’ve failed miserably lol
for _, myLetters in next, PlayersInActivation[Player]{}{} do - obviously doesn’t work

You’re trying to use table.insert() on a dictionary. You cannot do this.

To assign different datatypes and instances to dictionaries you need to declare A so you can define B:

local Dictionary  = {}
Dictionary["SovereignFrost"] = {"This is an example"}

By doing this, you’re declaring a table under Dictionary therefore you can index as so:

print(Dictionary["SovereignFrost"])
>>output This is an example

You can keep going so you can index A.B.C.D:

Dictionary["SovereignFrost"]["Data"] = {}
Dictionary["SovereignFrost"]["Data"]["Money"] = 0

If you’d want to remove a table from a dictionary then you can just declare that its nil:

Dictionary["SovereignFrost"] = nil
9 Likes

Yeah, I keep messing up with the differences of tables and dictionaries. Thank you

Try using _ to get the key of the dictionary

for i,v in next, PlayersInActivation[Player]["letters"] do

This should work.

Btw in your code where you check if the letters are in the table, you insert the letter for every value which isnt that letter. Let me explain.

{"a", "b", "c"}

Let’s say you have a table like this one, and you want to check if it contains the letter c. So you iterate it and check if myLetter == "c". BUT! The loop will first loop through a and b! And since "a" ~= "c" and "b" ~= "c", it will insert c twice into your table, even though it’s already in it.

In order to fix this, you can make a function which checks all values of the table, and then comes into a conclusion.

local function FindInTable(t, val)
    for i,v in pairs(t) do
        if v == val then
            return true --found!
        end
    end
    return false --the loop completed without returning, meaning not found!
end
1 Like