Making Changes to Arrays

Making Changes to Arrays

Table Functions

An array’s contents will often have to be changed, like when needing to remove an item from a player’s inventory. Lua has prebuilt functions for use with tables to make this easier.

The next sections will use functions to add, remove, and find items in a player’s inventory. For a complete list of functions to use with tables, see table API page.

Adding Values to Arrays

To add a new value to an array, use table.insert(array, valueToInsert). The second parameter can be any type of value such as a string, number, or even a Player object.

This example will add an item to a player’s inventory array when they’ve touched a part.

  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 Values 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, index). This function will either remove the last value of a table, or at a specific index depending on if both parameters are used.

Removing the Last Value

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

  1. Create an array named playerItems and insert three different string values if you haven’t already.
  1. To remove the last value, "Sleeping Bag", type table.remove(playerItems).
  1. Copy and paste the for loop below your code to print the contents of the array.
  1. Run the project. In the Output window, the last value, "Sleeping Bag", shouldn’t print.

Remove by Index

To remove a value at a specific point in the array, use the second parameter in 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 parameter for table.remove() only accepts 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

Searching for Values in an Array

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.

Finding 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. Use the search function by creating a variable named valueFound and calling findValue(). Pass in the array to search, and what value it should search for.