Gun Stats dictionnary fail

So I’m trying a basic gun system because I never done that and I created some String Values inside of ServerStorage here:


Each of the datas are empty because I will update it with the type of gun the player will take and equip.
Then I made a LocalScript inside of my 1st gun and tried to get the StringValues and then update it. However, I got a problem when I set the first value:
image
The “=” didn’t wanted to work but when I set it with “==” it worked even tho I wanted to update the value.
image
Can someone tell me if this is a mistake or if this is my fault ?

The first gunStats example indicates that you’re assigning a value using a statement (x = y). The second gunStats example simply stores the value of each expression (i.e., true, false). It seems that you’re trying to perform an assignment, so == is not correct.

If your intent in the LocalScript is to update a StringValue, you have to assign to GunStatsFold.FireRate.Value, rather than to the instance itself.

You’re also attempting to assign an integer (17) to a StringValue. Use a NumberValue instead, and consider making FireRate a NumberValue.

3 Likes

okay yes this was my mistake. thanks for clarifying :slight_smile:

1 Like

oh wait I set MaxAmmo as a NumberValue but I still can’t put an int in it
image
Sorry for the late answer

You are using array or dictionary table ?

array doesn’t need index to be named for it is force-number (and there is nothing you can about it)

likes this Gunstatsfold.MaxAmmo (it’s being as value while index is a which number)

dictionary needs key to be named or at least have reference to some instance and needs to be manually add value

likes this [Gunstatsfold.MaxAmmo]=17

for i,v in tabledictionary do
i.Value=v
end

Use roblox documentation as it has information about it (Array and dictionary)

2 Likes

You are needlessly wrapping your assignments in a table. The table constructor (the stuff contained within and including {}) does allow the usage of = inside of it, but that is extremely different from regular assignment. When constructing a table, the equals sign is used to set the keys of the table directly. By default, it assumes string keys, unless you wrap the key with [], which will then use the parsed expression contained inside the brackets.

local t = {
	Something = 1, -- "Something" is the string key
	["A Different Way"] = 2 -- You have to use brackets because of the spaces
}

is functionally equivalent to

local t = {}
t["Something"] = 1
-- or, equally
t.Something = 1
-- This format is required since spaces can't be used with the dot operator
t["A Different Way"] = 2

Here’s what you are (seeming to be) doing: You are making a table and trying to set its keys to Instance descendants. This does not change anything, because you are simply creating a new table with Instance keys. The actual Value objects aren’t changing because you’re not actually assigning to them, only accessing them.

Here’s what I think you want to do: You should directly assign to the entries’ Value members, as mentioned by others, in GunStatsFold. You don’t need to create a table, because the data is stored in the folder’s children. The syntax would be like GunStatsFold.MaxAmmo.Value = 17, without any surrounding table constructor.

If you want to store the stats in a table for some other utility, then just use string keys directly, as demonstrated above. Please do not do [GunStatsFold.MaxAmmo] = 17 or [GunStatsFold.MaxAmmo.Value] = 17 as these will be nonsense.


Additional Resources (at least skim these, please)

Programming in Lua, 2.5 - Tables
Programming in Lua, 3.6 - Table Constructors
Programming in Lua, 4.1 - Assignment
Programming Lua, fourth edition, Chapter 5. Tables (page 33)

3 Likes

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