Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Are arrays not working like the used to before?

Asked by 5 years ago
Edited 5 years ago


local Prices = { Lemonade = "100", "Lemonade" } function PRICECHECK(player, Price) print("a") for i, v in pairs(Prices) do print(v) if v == game.Players.LocalPlayer.Backpack.Data.CurrentItem.Value then print(v) return(v) end end end game.ReplicatedStorage.Buy.OnInvoke = PRICECHECK

In this script, the i, v in pairs goes through the array, I wanna return "100" and not "Lemonade". Instead it returns "Lemonade", what am I doing wrong?

0
Looks fine to me, since you printed v you'll see that it's 100 and not lemonade gullet 471 — 5y
0
your issue is that Lemonade: 100 and 1: Lemonade are both key-value pairs so it'll print both 100 and lemonade User#22604 1 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago

What you have there is what we call a dictionary. In it,you specify a name and its value.

local dictionary = { Lemonade = "100" }

If you try to do print(dictionary[1]) it will not work, as it's a dictionary and not an array. If you want to make it print "100" you'd simply have to do print(dictionary["Lemonade"]) or print(dictionary.Lemonade)

This is how an array can look like:

local array = {1,2,true, false, "hi", "world"}

If you then do print(array[1]) it will print 1.

If you want it to return the value of "Lemonade" in your script, simply do the following:

local value = Prices[v], then valuewill be "100"

0
It returns nil. Thepoint13 99 — 5y
0
What does. 1TheNoobestNoob 717 — 5y
0
A dictionary is still an array. Arrays hold integer keys only, whilst a dictionary may contain keys of any datatype except nil. Goulstem 8144 — 5y
Ad
Log in to vote
0
Answered by
Cyrakohl 108
5 years ago
Edited 5 years ago

The problem here is that you have made a dictionary instead of an array. But if you would like to index the dictionary you do : Dictionary[“Key”].

It’s very straight forward but if you don’t understand there are resources to help you:

https://www.robloxdev.com/articles/Table#Dictionaries

0
A dictionary is still an array. Arrays hold integer keys only, whilst a dictionary may contain keys of any datatype except nil. Goulstem 8144 — 5y

Answer this question