Coder10101
Coder10101

Reputation: 27

How to make a list variable?

I want to make a game where you buy upgrades, but you can only buy them once. To do this, I want to make a variable list that adds the name of the upgrade to the list when it's bought, and so when you try to buy the same upgrade, the script searches through the list and finds that upgrade name and tells you that it was already purchased.

For some reason I couldn't find any Roblox tutorials on a variable like this, but I am familiar with it in other programs.

Upvotes: 0

Views: 175

Answers (1)

Coder10101
Coder10101

Reputation: 27

Okay, I Did some research on the roblox site and I learned they use "arrays" which is similiar to list, so basically what I did is

i created an "array" (I made an empty list to start.)

local upgradesBought = {}

Then when I bought the upgrade I would add the upgrade name like this

table.insert(upgradesBought, upgradeName)

and then using the site below I used created a function to search for a certain name in the array like this

local function findValue(whichArray, itemName)
  for currentIndex = 1, #whichArray do
    if whichArray[currentIndex] == itemName then
      return currentIndex
    end
  end
end

Therefore after that all I had to do was check if I had already bought it.

local valueFound = findValue(upgradesBought, upgradeName)

if valueFound then
  -- Tell user that uprade has already been bought
else
  --Buy the upgrade
end

Here is the roblox site which will repeat what I just said and go much more in depth of the functions of an array Roblox Documentation: Arrays

Upvotes: 0

Related Questions