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

Table with Strings used as Values?

Hi all! What I want to do is take a table of strings, and use them as values. This way, I don't need two tables with variables, and then strings. Here's my table

local MenuValues = {"MainMenu", "Episodes"}

function GetMenu()
	for slot=1, #MenuValues do
		if (MenuValues[slot]) == true then --This is what I've got so far, but it doesn't work
			
		end
	end
end

I've done something something similar in the past, but it used global variables. Here it is for reference

Modes = {"Easy", "Normal", "Colored", "Hard", "Nightmare"}
function ResetMode()
	for i,v in pairs(Modes) do
		_G[Modes[i]] = false
	end
end

Any help is appreciated!

5 comments

0

What do you mean by 'use them as values' ? The problem with line 5 is that the index is a string, and you're trying to compare it with a bool. The value will never be 'true' and therefore your code never continue.

0

Please look at the example I show with global variables? Do I have to use global variables to do this again, or can I do it with inscript variables

1

@Goulstem Actually, Lua considers strings as true.

0

really o:

0

Neat! http://prntscr.com/c9glx3 :D I didn't know that.

1 answer

✓ Accepted answer

Frankly, strings aren't any value other than a collection of characters.

In your example with global variables, you use the strings as keys. The string is used to access a value. This is perfectly legitimate and can be done without global variables.

local dictionary = {
	a = 1,
	b = 2,
	c = 3
}

print( dictionary["a"] ) --> 1

Or, what you're specifically doing, taking the values of one table and making them the keys of a second table.

local values = {"a", "b", "c"}
local tab = {}

for i, v in ipairs(values) do
	tab[v] = false
end

This is the same as what you did, except it doesn't use global variables.

1 comment

0

Thank you very much!

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