Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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])

6 comments

0

it still comes out as nil

0

Hello otaku if thats your name, do this: class1.PrimaryAttachments ---inside of print (;

0

Okay, try doing print(class1["Primary"]) It should work 99%.

0

xxcoordinatorxx, but if he's trying to get this data from another script, that wouldn't be the wise choice to go by.

0

still comes out as nil

0

But he isn't right now so yeah

1 answer

✓ Accepted answer

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)
]]

3 comments

0

This is kinda what I just said, but he explained it better. Please use this method.

0

Thank you. And thanks to everyone that helped!

0

But thats what i said and the guy said i was wrong @xMonaitary why?

This discussion is preserved as it was. New answers, comments, and votes are closed.