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.
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.
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
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