Table returns nil?

Why’s the table returning nil?

Table

local Displayers = {
	CostOne = 100,
	CostTwo = 200,
	CostThree = 500,
	CostFour = 1e3,
	CostFive = 5e3,
	CostSix = 7e3,
	CostSeven = 10e3
}

Print

print(Displayers[1])
print(Displayers[2])
print(Displayers[3])

just use Displayers.CostOne instead of Displayers[1]

I could, I don’t want to do that for every cost/upgrade.

for that to work correctly, you must get rid of the Cost indicators.

If you don’t want to do that, then you’ll have to change the variables names or call it like @9_1kai stated.

1 Like

Oh, well that sucks. I’ll remove the cost indicators

ive actually found a way to fix that:

local Displayers = {
	CostOne = 100,
	CostTwo = 200,
	CostThree = 500,
	CostFour = 1e3,
	CostFive = 5e3,
	CostSix = 7e3,
	CostSeven = 10e3
}

Displayers[1] = Displayers.CostOne

print(Displayers[1])
1 Like

That’s just extra steps. If you really want to keep the index as a readability thing, you can always do this:

local Displayers = {
	[1] = 100,
	[2] = 200,
	[3] = 500,
	[4] = 1e3,
	[5] = 5e3,
	[6] = 7e3,
	[7] = 10e3
} -- the same as {100, 200, 500, ...}
2 Likes

Ty for the script! and ty to all that helped. Now I can do this:

Remotes.Count.OnClientEvent:Connect(function(amount: number)
	local Count = amount
	
	Cost.Text = tostring(Number_Abbreviations(Upgrades[Count]))
	
end)

Lua tables have two parts: an array part, and a dictionary (unordered map) part. You’re creating your Displayers table as a dictionary with string keys. This table has no array part, so you can’t use natural number indices to access anything you put into it. As far as Lua is concerned, the order of your entries also does not need to be preserved, so if you iterate with pairs, the values are not guaranteed to print in the order you specified (they probably will, but the language standard itself does not guarantee it).

A Lua table has an array part only if things are put into it using natural number indices starting at 1, either explicitly, or by using the array literal notation, i.e. array = { thing1, thing2, thing3 }. The array part will cover only entries for consecutive numbers too, like, 1, 2, 3, 4, 5, etc… if you make a table and only put in indices 1, 2, 4, 5, 6… the array part will only be entries for 1 and 2. Indices 4, 5, and 6 will be treated as dictionary keys, in the dictionary part of the table, inaccessible to ipairs(). This is a common Lua pitfall with the ipairs iterator and # operator, like if you have an array with indices 1, 2, 3, 4, 5… and at some point the 3rd element in the array becomes nil, it breaks the array into an array of 1, 2, and a dictionary of 4, 5.

Run the following Lua in Studio and you’ll see the issue:

local t = { "A","B","C","D","E" }
print("before:")
for i,v in ipairs(t) do
    print(i,v)
end

t[3] = nil

print("after:")
for i,v in ipairs(t) do
    print(i,v)
end

The biggest pitfall is when you make an array of things, and some other code makes one of the items in the array become nil, after which ipairs will no longer iterate over all of the remaining items (since some are now in the dictionary). The # array size operator also does not count things in the dictionary part.

1 Like

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