How could I access a specific item in a table? [solved]

edit: Through a quick google search I discovered the solution: increases[(index)] which has worked perfectly

local currentTool = game.Players.LocalPlayer.CurrentTool --accessing a leaderstat that determines the current tool

local increases = {
	"1";
	"2";
	"5";
	"10";
	"25"
}

Basically, the script is hooked up to an upgrade system that determines how much stats are given when the tool is clicked. However, I was wondering if I could select a specific value. Something like this:

local statIncrease = (item currentTool.Value of table)
And from there, it can be linked up to the tool

you can index the i’th element of the table using increases[i]

It’s very simple, as well as there are multiple ways to do so.

table.find() being one of them. You would use it like this:

local statIncrease = table.find(increases, "25") -- returns increases[25] (which is also what I'm about to show)

You can also use square brackets after the table name to identify it with the name, or the index.

local statIncrease = increases[5 --[[the 5th items in the table]]] -- returns 25

--[[Or you can do it like this:]] local statIncrease = increases[#increases] -- the last thing in the table. In this case, it would be "25"

You can also change the table to have string names, like variables, then refer to them that way.

local increases = {
	First = "1";
	Second = "2";
	Third = "5";
	Fourth = "10";
	Sixth = "25"
}

local statIncrease = increases.Sixth -- returns 25

You can also use a for loop, then in if statement inside of that to check. Don’t use this one for what you’re trying to accomplish. It won’t work for that. This one is just an example of what you can do for the future.

local increases = {
	"1";
	"2";
	"5";
	"10";
	"25"
}

local statIncrease

for i, v in pairs(increases) do
	print(i) -- prints the index
	print(v) -- prints the value in the table

	if v == "25" then
		statIncrease = v -- sets the "statIncrease" variable to 25 if the current value the loop is on is equal to very specifically "25" in a string.
	end
end

I would also recommend changing the values in the table to numbers instead of strings. That will cause issues later if you don’t use tonumber().

2 Likes

table.find() returns the index not the value, table.find(increases, “25”) would return 5 not 25. To reference it you’d do

increases[table.find(increases, "25")]

Would there be a way to have this apply to whatever item on the list is equal to the statIncrease variable. An item on the list would be, for example, the third item on the list which is 5, not specifically one of the strings?

Ah, my bad. You are correct.

Character limit

You just change it accordingly to what you need. You just do increases[index] to get a specific one out of the table. You could also randomize the selection using increases[math.random(1, #increases)].


The index is just the number of something in a table. Sort of like a specific key for something in that table.

local increases = {
	"1"; -- index = 1
	"2"; -- index = 2
	"5"; -- index = 3
	"10"; -- index = 4
	"25" -- index = 5
}

And for what you asked for specifically, yes, but I do not have any time to explain at the current moment.

You can use the table.find() event. It looks like this:

table.find(--the name of the table, --the index in the table you want to access)
1 Like