How to access this array data

I have an array and, but when I try to access the array data, it just doesnt appear.
For example:

local Pets = {
{“Shadow”, 1.5, 1.2, 1000, 19283},
{“Unicorn”, 2, 1.5, 5000, 19283},
{“Pegasus”, 3, 2.5, 15000, 19283}
}

This is my array but when I do, Name.Text = Pets[1][1] it comes up with 1.5 instead of the name “Shadow” How do I get it to do the name instead of the 1.5.

2 Likes

Can you show the script you are using and not the example, because the example you showed works fine for me.


(Also this post should be in #help-and-feedback:scripting-support )

Apologies for the wrong section, my bad.

local menu = script.Parent
local info = script.Parent.Parent.Petinfo
local button1 = script.Parent.Button1
local button2 = script.Parent.Button2
local button3 = script.Parent.Button3

-- Arrays
local Pets = {
	{"Shadow", 1.5, 1.2, 1000, 19283},
	{"Unicorn", 2, 1.5, 5000, 19283},
	{"Pegasus", 3, 2.5, 15000, 19283}
}



local function onClick()
	
	if button1.ID.Value == 1 then
		info.Info.petname.Text = Pets[1][0]
		info.Info.Coin.coinmulti.Text = Pets[1][1]
		info.Info.Gem.gemmulti.Text = Pets[1][2]
		info.Info.Cost.price.Text = Pets[1][3]
		info.Info.ImageLabel.Image = Pets[1][4]
	elseif button1.ID.Value == 2 then
		info.Info.coinmulti.Text = Pets[2][1]
		info.Info.gemmulti.Text = Pets[2][2]
		info.Info.price.Text = Pets[2][3]
		info.Info.Image = Pets[2][4]
	elseif button1.ID.Value == 3 then
		info.Info.Coin.coinmulti.Text = Pets[3][1]
		info.Info.Gem.gemmulti.Text = Pets[3][2]
		info.Info.Cost.price.Text = Pets[3][3]
		info.Info.ImageLabel.Label = Pets[3][4]	
	end
end

It seems like you are used to another language where the index of the first value in an array is 0.

--          1   2   3
local t = {"e", 6, true}

print(t[1]) -- "e"
print(t[0]) -- nil

This means that your onClick function should look something like this:

local function onClick()
	
	if button1.ID.Value == 1 then
		info.Info.petname.Text = Pets[1][1]
		info.Info.Coin.coinmulti.Text = Pets[1][2]
		info.Info.Gem.gemmulti.Text = Pets[1][3]
		info.Info.Cost.price.Text = Pets[1][4]
		info.Info.ImageLabel.Image = Pets[1][5]
	elseif button1.ID.Value == 2 then
		info.Info.coinmulti.Text = Pets[2][2]
		info.Info.gemmulti.Text = Pets[2][3]
		info.Info.price.Text = Pets[2][4]
		info.Info.Image = Pets[2][5]
	elseif button1.ID.Value == 3 then
		info.Info.Coin.coinmulti.Text = Pets[3][2]
		info.Info.Gem.gemmulti.Text = Pets[3][3]
		info.Info.Cost.price.Text = Pets[3][4]
		info.Info.ImageLabel.Label = Pets[3][5]	
	end
end
1 Like

Thank you so much! This worked

1 Like

is there a reason why you’re using an array of pets over a dictionary?

local Pets = {
  ["shadow"] = {
    ["name"] = "Shadow",
    ["coin_multiplier"] = 1.5, 
    ["gem_multiplier"] = 1.2, 
    ["price"] = 1000, 
    ["unknown"] = 19283, -- Not sure what this is since it's not an image
    ["image"] = "rbxassetid://0"
  };
  ["unicorn"] = {
    ["name"] = "Unicorn",
    ["coin_multiplier"] = 2, 
    ["gem_multiplier"] = 1.2, 
    ["price"] = 5000, 
    ["unknown"] = 19283, 
    ["image"] = "rbxassetid://0"
  };
  ["pegasus"] = {
    ["name"] = "Pegasus",
    ["coin_multiplier"] = 3, 
    ["gem_multiplier"] = 2.5, 
    ["price"] = 15000, 
    ["unknown"] = 19283, 
    ["image"] = "rbxassetid://0"
  };
}

-- ^ Pets could be a ModuleScript instead
-- local Pets = require(ReplicatedStorage.Pets)

local function onClick()
  local TargetPet = Pets[Button1.ID.Value:lower()] -- Should be a StringValue instead
  info.Info.petname.Text = TargetPet.name
  info.Info.Coin.coinmulti.Text = TargetPet.coin_multiplier
  info.Info.Gem.gemmulti.Text = TargetPet.gem_multiplier
  info.Info.Cost.price.Text = TargetPet.price
  info.Info.ImageLabel.Image = TargetPet.image-- I'm assuming you removed the images from Pets

  -- info.Info is a bad way to name things
  -- you could use a for loop if you named your stuff properly 
  --[[
  for _, PetProperty in info.Info:GetChildren() do 
    if not PetProperty:GetAttribute("IsPetProperty") then continue end
    PetProperty.Text = TargetPet[PetProperty.Name] 
  end
  ]]
end

You can optimize your code to look more clean like this. (This is just an example, it may not work in your case)

local function onClick()
	local id = button1.ID.Value

	info.Info.petname.Text = Pets[id][1]
	info.Info.Coin.coinmulti.Text = Pets[id][2]
	info.Info.Gem.gemmulti.Text = Pets[id][3]
	info.Info.Cost.price.Text = Pets[id][4]
	info.Info.ImageLabel.Image = Pets[id][5]
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.