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

My table won't get the data inside but why?

Asked by 5 years ago

I'm trying to make a call of duty FPS type game and this is for the class but I'm not really good at tables and when I try this the output comes out as nil. Any help would be appreciated.

local class1 = {
    Primary = "AN-94";
        PrimaryAttachments = "Reflex Sight";
        PrimaryAttachments2 = "Laser Sight";
        PrimaryAttachments3 = "Quickdraw";


    Secondary = "M1911";

    equipment = "Frag Grenade";
    tactical = "Tactical Insertion";

    Perk1 = "Sleight of Hand";
    Perk2 = "Lighweight";
    Perk3 = "Dead Silence"
}

local class2 = {
    Primary = "SCAR-L";


    Secondary = "M1911";

    equipment = "Frag Grenade";
    tactical = "Tactical Insertion";

    Perk1 = "Sleight of Hand";
    Perk2 = "Lighweight";
    Perk3 = "Dead Silence"
}

local activeclass = class1

print(class1[2])
0
it still comes out as nil GameBoyOtaku 63 — 5y
0
Hello otaku if thats your name, do this: class1.PrimaryAttachments ---inside of print (; xxcoordinatorxx 135 — 5y
0
Okay, try doing print(class1["Primary"]) It should work 99%. B_rnz 171 — 5y
0
xxcoordinatorxx, but if he's trying to get this data from another script, that wouldn't be the wise choice to go by. B_rnz 171 — 5y
View all comments (2 more)
0
still comes out as nil GameBoyOtaku 63 — 5y
0
But he isn't right now so yeah xxcoordinatorxx 135 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

The reason it comes out as nil is you have altered the index.

Here is an example of what I'm talking about

local Tab = {NewIndexName = 'This is a string value', TestIndex = 2, ['Hacked Index'] = math.pi}

for Index, Value in pairs (Tab) do
    print('Index: '..Index..; Value: '..Value)
end

If you wanted to print the value, you have to access it by it's index, so if you have changed the index from numbers to a string value then you have to use the new string value you set on the value

local Tab = {NewIndexName = 'This is a string value', TestIndex = 2, ['Hacked Index'] = math.pi}

print(Tab['NewIndexName']) -- this will work for all cases

print(Tab.TestIndex)
--[[
or alternatively you could use this, although this may not work for all cases, such as the 'Hacked Index' since there is a space and the way you are retrieving the value in the table is not a string value )which you cannot use a space when you are not using a string, it will through you an error in the output)
]]
0
This is kinda what I just said, but he explained it better. Please use this method. B_rnz 171 — 5y
0
Thank you. And thanks to everyone that helped! GameBoyOtaku 63 — 5y
0
But thats what i said and the guy said i was wrong @xMonaitary why? xxcoordinatorxx 135 — 5y
Ad

Answer this question