Accesing table return nil

Hi im trying to acces a table value and when i print it returns nil, whats going on?

tracks = {
	"lola"
	
}

print(tracks["lola"])

image

What you have shown is an array, when you are treating it as a dictionary.

A dictionary allows you to access values by keys:

local Dict = {
    ["Apples"] 5,
    ["Dogs"] = 2,
    ["Name"] = "HOLAGD_YT"
}

print(Dict["Apples"]) -- 5
print(Dict["Dogs"]) -- 2
print(Dict["Name"]) -- HOLAGD_YT

while a table allows you access values by index (numerical keys)

local Table = {
    "Apples",
    "Dogs",
    "Name"
}
print(Table[1]) -- Apples
print(Table[2]) -- Dogs
print(Table[3]) -- Name

To find if a value exists in a table, you can use table.find(TABLE, VALUE)

i.e.

if table.find(tracks, "lola") then
    -- code goes here
end
1 Like

thansk!!! now how can i create keys from script?

i was using table.insert to insert a text value

random = math.random(1, #tracksFolderOnTable)|
local text = tracksFolderOnTable[random].Name|
table.insert(tracksTable, {text})|

You can just do this:

local dict = {}
dict["Apples"] = 5

print(dict["Apples"]) -- 5
1 Like

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