Printing out a dictionary scrambles keys

very simple issue, that i unfortunately do not know how to fix

this is one table:
image

when i print its contents, everything is mixed up:
image


meaning, if i try to get the buff value (which is index 3), it will instead return… nil?

for _, value: IntValue in gameplayValues:GetDescendants() do
		if value.Name ~= assignedValue then continue end
		
		local traitBuff: number = traitStats[trait][3] -- 3rd index is always the buff value (number)
		warn(traitStats[trait])
		warn("Applying " .. trait .. " to " .. value.Name)
		if traitStats[trait].isMultiply then
			if remove then
				value.Value = math.floor(value.Value / traitBuff)
			else
				value.Value = math.floor(value.Value * traitBuff)
			end
		else
			if remove then
				value.Value -= traitBuff
			else
				value.Value += traitBuff
			end
		end	
	end


EDIT: for now, i managed to find a workaround to this, but i’m leaving this as unsolved incase anyone knows what’s causing this

i just did this:

local traitBuff: number 		
		for i, value in traitStats[trait] do
			if typeof(value) == "number" then
				traitBuff = value
			end
		end

can’t you just do traitStats[trait].meleeDamageBonus? Dictionary uses defined string as key, not number like array.

nvm i get what you mean now. But still, Dictionary uses defined string as key, there won’t be any number index in it.
So here’s my suggestion:

Trait = {
 bla = "",
 buff = {"meleeDamageBonus",1.10}
}

local buffName,amount  = table.unpack(Trait.buff)

the issue is that i have “Extremely dumb syndrome”, and i can’t just do that, because:
image

would it be better to contexualize the bonus and have two keys, bonus and bonusType, instead of one key for a specific bonus that doesn’t exist in all of them?

1 Like

elaborate?

i might be a little confused rn

basically, your buffs could be put like this:

local buffs = {
	Brutal = {
		description = "You do not mess around in melee combat.",
		buff = "+10% melee damage",
		bonus = 1.10,
		bonusType = "damage",
		isMultiply = true
	},
	Swift = {
		description = "Even under pressure, you remain swift with your reloads.",
		buff = "+15% reload speed",
		bonus = 1.15,
		bonusType = "reloadSpeed",
		isMultiply = true
	},
	... etc
}

then, you can still index .bonus to get how much it would add to the certain statistic, and that statistic is determined by .bonusType, which you can make it check for for things like damage, reloadSpeed, aimSpeed, etc…

if you don’t want to do this you can still always check if they exist like this:

if traitStats[trait].meleeDamageBonus then
	...
end
if traitStats[trait].reloadSpeedBonus then
	...
end

but in my opinion, the first idea i proposed would be cleaner and more elegant

1 Like

yeah i can see this working, thanks vro

1 Like