As @tamasjonas mentioned, you're using a dictionary. This means the 'id/key' for your data is defined by you rather than an ordinary list which goes from 1++.
The reason you can't just use local bag = Player.bagValue.Value is because all that gives you is your key not the value inside of this you're looking for : ). This line of code:
local bagInfoIndividual = bagInfo[bag]
simply takes a key ( bag i.e Player.bagValue.Value) which in your case is either bag1 or bag2 then finds the attached data, 5 and 25 respectively!
(edit) An Example:
local dictionary = {
[keyName] = value,
[keyName] = value
}
All dictionaries follow this structure, they can get more complicated than this with dictionaries inside each other but this is it at its most basic, one level deep.
If the values & keys for the above example were:
local colours = {
[green] = BrickColor.new("Medium green"),
[red] = BrickColor.new("Tr. Red"),
}
part.BrickColor = colours[green]
wait(2)
part.BrickColor = colours[red]
You can think of it a bit like variables if these variables were contained in a table. We have an id and value just like varaibles except we're accessing them through a table (dictionary) which makes them more powerful and useful in some cases.
For instance if you wanted to set the bricks colour to 5 preset colours, instead of doing it like this:
local redColor = value
local yellowColor = value
local greenColor = value
local blueColor = value
part.BrickColor = redColor
wait(2)
part.BrickColor = yellowColor
You can just create a dictionary then loop over it like so:
(fun fact, when you do: instance:GetChildren() it returns a dictionary)
for colorName, brickColor in pairs(colours) do
part.BrickColor = colorValue
wait(2)
end
2 comments
The thing is, as im reading your explanation for my example, I understand it, but if I will try to impliment it into another code, I know that i will get so lost, do you think you could give me like another example on how it works, not with my code, eg not with bag1, If that's fine with you, Thanks!
@EEnergy, Hey! I made some edits with a deeperr breakdown and new example : )