Local script table?

When I do this in a local script it will print “Not good”, as you can see in the script. The players currentBubble value = 0, so I dont know what the problem is?

local player = game.Players.LocalPlayer

wait(5)

local bubbleTable = {
	["BlueBubble"] = 5000,
	["PurpleBubble"] = 15000,
	["PinkBubble"] = 50000,
}

if bubbleTable[player.Values.CurrentBubble.Value + 1] then
	print("Good!")
else
	print("Not good")
end

btw this is save for cheating, I’m using this for the interface only

There are 2 issues that I can currently spot, which are:

  1. Since it’s a LocalScript, you’ll need to use the WaitForChild method to wait for the Instances to finish loading, otherwise they can be nil when indexed
  2. The bubbleTable is a dictionary since its keys are a string value, and you seem to be indexing it using a number value, which will always result in nil since there aren’t any numerical keys present. Consider changing the bubbleTable into an array if you prefer to index tables using number values instead
1 Like

I don’t really understand what you mean. I’ve tried putting .key after the od statement id thats what you mean

1 Like

This article from the docs should help explain the difference between dictionaries, arrays and mixed tables:

Like @JohhnyLegoKing said, you are using string keys for your table and then attempting to reference them with a number.

It seems like you are getting mixed up with the keys.
When you make an array, like this:

local myArray = {
    "hi",
    "bye",
    "how are you today"
}

Lua actually formats it into this structure:

local myArray = {
    [1] = "hi",
    [2] = "bye",
    [3] = "how are you today"
}

This allows you to do things like print(myArray[1]), which would output “hi”.

The item on the left is the key, and the item on the right is the value, hence why you give two variables when iterating over it.

--ipairs() for numerical keys
--pairs() for string keys
for key, value in ipairs(myArray) do
    print(key, value)
end

In your case, you have string keys.

local bubbleTable = {
	["BlueBubble"] = 5000,
	["PurpleBubble"] = 15000,
	["PinkBubble"] = 50000,
}

To reference the first item in your array, you would do print(bubbleTable.BlueBubble), which would output 5000.

In short:

  • The item on the left is the key, the item on the right is the value which is associated with the key.
  • When we reference items in an array we reference the key which is associated with the value we are trying to access.
  • Keys can be numerical or string:
    Numerical keys use [1] (and etc.), things like GetChildren() and GetDescendants() return a numerical array.
    String keys use a string. To reference the third item in your array, we would use bubbleTable.PinkBubble.
5 Likes

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