Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Multi-leveled table help?

Asked by 8 years ago

So I have a table with multilevels of data, I'm trying to access the data in this table but it's giving me an this error.

14:16:11.859 - Players.Player1.PlayerGui.Screen.Main.Control:89: attempt to concatenate field '?' (a nil value) 14:16:11.860 - Stack Begin 14:16:11.860 - Script 'Players.Player1.PlayerGui.Screen.Main.Control', Line 89 14:16:11.860 - Stack End 14:17:58.465 - Auto-Saving...

Heres my table:

Upgrades = {
    Cursor = {
        ImageID = 12312414,
        Name = "Cursor",
        Cost = 15,
        CPS = .1
    }
}

And heres my code accessing the table

for i, v in pairs(Upgrades) do
    local upgrade = game.ReplicatedStorage.Upgrade:Clone()
    upgrade.Parent = gui.Main.Upgrades
    --upgrade.Position = UDim2.new(0, 0, 0, (i - 1) * 75)
    upgrade.ImageLabel.Image = "http://www.roblox.com/Asset?ID="..v[1] --this is line 89 right here
    upgrade.UpgradeName.Value = v[2]
    upgrade.Upgrades.Value = 0
    upgrade.UpgradeCost.Value = v[3]
    UpdateUpgrades()
end

TL;DR: How do I access data in a multileveled table?

1 answer

Log in to vote
2
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
8 years ago

Your Problem

When indexing a table like table[index] then the 'index' is essentially the 'tag' of whatever index you're getting. By default.. 'tags' are in numerical order from least to greatest. But when you apply keys to tables like;

local tab = {
    a = 1
}

Then the key overwrites the default tag. tab[1] is now nil, and can only be accessed by doing tab['a'] or tab.a


How To Fix

You need to either eradicate the custom key tags from your multi-leveled table or actually use them(lol)


Code

Upgrades = {
    Cursor = {
        ImageID = 12312414,
        Name = "Cursor",
        Cost = 15,
        CPS = .1,
     }
}

for i, v in pairs(Upgrades) do
    local upgrade = game.ReplicatedStorage.Upgrade:Clone()
    upgrade.Parent = gui.Main.Upgrades
    --upgrade.Position = UDim2.new(0, 0, 0, (i - 1) * 75)
    upgrade.ImageLabel.Image = "http://www.roblox.com/Asset?ID="..v.ImageID
    upgrade.UpgradeName.Value = v.Name
    upgrade.Upgrades.Value = 0
    upgrade.UpgradeCost.Value = v.Cost
    UpdateUpgrades()
end
Ad

Answer this question