We use cookies on this site to enhance your user experience

Making Changes to Arrays

10 min

Making Changes to Arrays

Person getting potion, adding to an array

Table Functions

An array’s contents will often have to be changed, like removing an item from a player’s inventory, or adding a player’s name to list. Lua has prebuilt functions to make changes to all types of tables, including arrays.

To use these functions, type table. and a function name, like in table.insert(). For instance, in the example below, the insert() function adds a part into the table, myPartArray.

The next section will use code snippets to add, remove, and find items in a player’s inventory and display the table contents through the Output Window.

For a complete list of functions to use with tables, see table API page.

Adding to Arrays

Sometimes you’ll need to add values to an array while the game is running, such as adding an item to a player’s inventory when they’ve touched a part.

To add a new value to an array, use table.insert(array, valueToInsert). The second parameter can be anything stored normally in a variable, e.g.a string, number, or even a Player object.

  1. Create an empty array named playerItems.
  1. Add inventory items to the array by typing table.insert(). In the parenthesis, type playerItems followed with a string for each item. Insert at least three values.
  1. To print all the array values in the Output Window, copy and paste the for loop below into your script.
Combining Strings and Variables

To combine a string with a variable or other strings, a process called concatenation, type .. between the string and the variable name.

  1. Run the project. You should see each item printed in the Output Window.
Inserting Different Value Types

Remember that you can insert other values using table.insert(). Some examples include:

  • table.insert(numberArray, 10)
  • table.insert(partArray, Workspace.Part1)
  • table.insert(playerArray, RobloxPlayer)

Removing from Arrays

To remove a value, like if a player used an item or someone in a list of active players leaves a game, use table.remove(arrayName). Depending on the parameters given, this function can remove values in different ways.

Remove the Last Value

To remove the last value in an array, type table.remove(arrayName).

  1. Create an array named playerItems with at least three different string values.
  1. To remove the last value, "Sleeping Bag", type table.remove(playerItems).
  1. Copy and paste the code below to print out the array contents.
  1. Run the project. In the Output Editor, the last value, "Sleeping Bag", shouldn’t print.

Remove by Index

To remove a value at a specific point in the array, add a second parameter, the index, to table.remove(arrayName, whichIndex).

  1. Use an array named playerItems with at least three string values.
  2. Type table.remove(). To remove the first value, in the parenthesis, type playerArrays, 1.
Table.Remove() only Accepts Indexes

The second argument in table.remove() must be a numerical index. Typing something like table.remove(playerItems, "Bread") will create an error.

  1. Print out the array using the code below.
  1. Run the project. Check that the first value, "Potion" was removed.
Notice also that all the index values changed. Removing a value from an array will change the array size and shift index values down after the one removed. "Bread" is now index 1, and "Sleeping Bag" is now index 2.
Before Removing
Index 1: Potion
Index 2: Bread
Index 3: Sleeping Bag
After Removing at Index 1
Index 1: Bread
Index 2: Sleeping Bag

Finding Values

To find specific values in arrays, like the name of a winning player, you’ll need to code your own search function using for loops and if statements. There isn’t a built-in function.

Find a Single Value

To find a value in an array, create a function named findValue() that goes through an array and stops the first time it finds a matching value.

Once it finds the value, the function will use the keyword return to send back that value’s index With an index, you can do tasks like remove it from the array.

  1. Copy the array named playerItems below.
  1. Code a new function named findValue() with two parameters:
  • whichArray - the array to search through.
  • itemName - the specific string to check for.
  1. In findValue(), use a for loop to check if any value in the array matches itemName. If the value matches, use the keyword return to send back the value of currentIndex.
  1. Store the index found by creating a variable named valueFound and calling the search function, findValue(). For the arguments, use the name of the array to search, and what value it should be searching for.
Using return In a Function

return will stop a function and take the script back to the line where the function was originally called. If there is something after the keyword return, that information is sent back through the function.

In this case, return brings back the value of currentIndex and stores it in the original variable where findValue() was called.

Use a Found Value

If a value was found using the find function, then it can be removed. So the script doesn’t try to remove a value that doesn’t exist and create an error, check if a value was found with an if statement.

  1. To check if a value is inside indexToRemove, use an if statement. In the statement, remove the found value by typing table.remove(playerItems, indexToRemove).
  1. Print out the array using the code below.
  1. Run the project and check that the first "Bread" was removed from the array, but the second one is still there. To further test the script, try removing other values by changing the second parameter in findValue().

Before removing the first "Bread"

After removing

Because this function was called once, it will only remove the first "Bread" it finds. The next section will cover how to find and remove all instances of a function.

While the function findValue() works with string and number values, be careful with the specific values you compare. For example, if you had a player, you’d need to check their Name value using the comparison:

player.Name == "playerNameToCheck" 

Find and Remove All of a Value

While the previous code could only remove the first instance of value found, this code snippet will find and remove all occurrences of from an array.

Remember, removing items causes later indexes to shift. Instead of starting at the beginning of the array, start at the end to avoid accidentally skipping values. By starting at the last index, you won’t change the indexes of the values before it.

  1. Use an array named playerItems with at least four values and a set of duplicates.
  1. To go through the array, create a for loop that goes backwards through playerItems, starting at #playerItems, ending at 1, and incrementing by -1.
  1. In the loop, remove a "Bread" value with an if statement that checks if the value inside playerItems[index] is equal to "Bread".
If found, use table.Remove(playerItems, index) to remove that value.
  1. Use the code below to add a second for loop that prints the array.
  1. Run the script and check that all valued named "Bread" are removed.

Finished Project Sample

Project File

The following project includes all scripts in this tutorial. Download here.

Note, all scripts are in ServerScriptService and disabled. To use a script, in its properties, uncheck the Disabled field and run Studio.

Previous Looping Through Arrays Next Intro to Module Scripts