Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Are arrays not working like the used to before?



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?

2 comments

0

Looks fine to me, since you printed v you'll see that it's 100 and not lemonade

0

your issue is that Lemonade: 100 and 1: Lemonade are both key-value pairs so it'll print both 100 and lemonade

2 answers

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"

3 comments

0

It returns nil.

0

What does.

0

A dictionary is still an array. Arrays hold integer keys only, whilst a dictionary may contain keys of any datatype except nil.

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

1 comment

0

A dictionary is still an array. Arrays hold integer keys only, whilst a dictionary may contain keys of any datatype except nil.

This discussion is preserved as it was. New answers, comments, and votes are closed.