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

Can Someone help me with the [] Syntax, I don't understand the Use of It?

Asked by
Borrahh 265 Moderation Voter
3 years ago

Hey everyone, I am making a click game, there are 2 bags. If the player has the bag1, he can have 5 clicks only, if bag2, 25 clicks.

bagValue = StringValue clicks = IntValue

The code perfectly works, But I don't understand at this part?

 local bagInfoIndividual = bagInfo[bag]

Someone helped me write this code, why do I need to create another variable for bagInfo[bag] can't I just use the local bag = Player.bagValue.Value

Thanks

bagInfo = {
  ["bag1"] = 5;
  ["bag2"] = 25;
}
function addToBackpack(Player)
  local bag = Player.bagValue.Value
  local clicks = Player.Clicks
  local bagInfoIndividual = bagInfo[bag]
  if clicks.Value <= bagInfoIndividual then
    clicks.Value += 1
  else
    warn("Cant have more")
  end
end

2 answers

Log in to vote
1
Answered by
JedDevs 20
3 years ago
Edited 3 years ago

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"),
}

-- we can now grab the BrickColor value of each of these colours and assign them
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

-- etc.

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
0
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! Borrahh 265 — 3y
0
@EEnergy, Hey! I made some edits with a deeperr breakdown and new example : ) JedDevs 20 — 3y
Ad
Log in to vote
1
Answered by 3 years ago
Edited 3 years ago

Replace the ";" with the ",". That [] thing in a list is called a dictionary. (Reply on EEnergy8 on @JedDevs Comment):You may try using this The script that handles it:

bagInfo = {
  ["bag1"] = 5;
  ["bag2"] = 25;
}
local bagInfoValue = Instance.new("StringValue")
bagInfoValue.Name = "BagInfo"
bagInfoValue.Parent = game.ServerStorage
bagInfoValue.Value = game:GetService("HttpService"):JSONEncode(bagInfo)

The reciving script:

bagInfo = game:GetService("HttpService"):JSONDecode(game.ServerStorage:WaitForChild("BagInfo").Value)
--Do the rest on this script.

Answer this question