"Child" table can't get a value that is from a "parent" table?

Hello! I was trying to make a variable inside a table that is called “attack_speed” that is inside a weapon’s stats table. Then, for a combo system i made a table named “sequence”. When i try to get the “attack_speed” value, it just can’t! What would be a workaround for this? I really need to get the value that is in the parent’s table because it’s used as a metatable, and i need that value to be dynamic

(i deleted unnecesary information)

local ItemSettings = {
	["Basic Sword"] = {
		attack_speed = 0.5, -- THE VALUE I WANT TO GET

		primary_function = {
			name = "MeleeAttack",

			sequence = {
				[1] = {
					anim = "Sword1_Attack1",
					anim_speed = 1 * attack_speed, -- TRIES TO GET THE VALUE BUT CAN'T
				},
				[2] = {
					anim = "Sword1_Attack2",
					anim_speed = 1,
				},
			},
		},
3 Likes

To fix this, you can separate the definition of attack_speed from the sequence table and define it before the ItemSettings table. Here:

local attack_speed = 0.5 -- Define attack_speed before ItemSettings

local ItemSettings = {
	["Basic Sword"] = {
		attack_speed = attack_speed, -- Assign the value of attack_speed

		primary_function = {
			name = "MeleeAttack",

			sequence = {
				[1] = {
					anim = "Sword1_Attack1",
					anim_speed = 1 * attack_speed,
				},
				[2] = {
					anim = "Sword1_Attack2",
					anim_speed = 1,
				},
			},
		},
	},
}
2 Likes

Its not ideal, because i have multiple objects that will use different attack speed, something like this:

local ItemSettings = {
	["Basic Sword"] = {
		SLOT_SPACE_X = 2,
		SLOT_SPACE_Y = 3,

		VIEWPORT_CAMERA_CFRAME = CFrame.new(0, 3, 7) * CFrame.fromOrientation(math.rad(0), math.rad(0), math.rad(-22.5)),

		EQUIPABLE_SLOT = "Weapon",
		Rarity = "Very uncommon sword",

		attack_speed = 0.5,

		primary_function = {
			name = "MeleeAttack",
			combo_timeout = 2,

			sequence = {
				[1] = {
					duration = 0.6,
					damage_multiplier = 0.8,
					anim = "Sword1_Attack1",
					anim_speed = 1 * attack_speed,

					cast_delay = 0.2,
					cast_duration = 0.2,
				},
				[2] = {
					duration = 0.5,
					damage_multiplier = 0.6,
					anim = "Sword1_Attack2",
					anim_speed = 1,

					cast_delay = 0.1,
					cast_duration = 0.2,
				},
				[3] = {
					duration = 1.2,
					damage_multiplier = 2,
					anim = "Sword1_Attack3",
					anim_speed = 1,

					cast_delay = 0.3,
					cast_duration = 0.2,
				}
			},
		},

		anim_idle = "Sword1_Idle",
		uses_melee_hitbox = true,

		EquippedStats = {
			Damage = 100,
			CriticalChance = 0.2,
		},
	},
	["Dagger"] = {
		SLOT_SPACE_X = 1,
		SLOT_SPACE_Y = 3,

		--VIEWPORT_CAMERA_CFRAME = CFrame.new(0, 3, 7) * CFrame.fromOrientation(math.rad(0), math.rad(0), math.rad(22.5)),

		EQUIPABLE_SLOT = "Weapon",

                attack_speed = 0.5,
		primary_function = {
			name = "MeleeAttack",
			combo_timeout = 2,

			sequence = {
				[1] = {
					duration = 0.3,
					anim = "Sword1_Attack1",
					anim_speed = 1 * attack_speed,

					cast_delay = 0.3,
					cast_duration = 0.2,
				},
			},
		},

		anim_idle = "Sword1_Idle",
		uses_melee_hitbox = true,

		EquippedStats = {
			Damage = 50,
			CriticalChance = 0.1,
		}
	},
	["Lil Knife :D"] = {
		SLOT_SPACE_X = 1,
		SLOT_SPACE_Y = 1,

		--VIEWPORT_CAMERA_CFRAME = CFrame.new(0, 3, 7) * CFrame.fromOrientation(math.rad(0), math.rad(0), math.rad(22.5)),

		EQUIPABLE_SLOT = "Weapon",


		attack_speed = 0.5,

		primary_function = {
			name = "MeleeAttack",
			combo_timeout = 2,

			sequence = {
				[1] = {
					duration = 0.3,
					anim = "Sword1_Attack1",
					anim_speed = 1 * attack_speed,

					cast_delay = 0.3,
					cast_duration = 0.2,
				},
			},
		},

		anim_idle = "Sword1_Idle",
		uses_melee_hitbox = true,

		EquippedStats = {
			Damage = 25,
			CriticalChance = 0.05,
		}
	},

Every weapon uses different attack speed :confused:

2 Likes

It’s not ideal, but it still works right?
Why not make a variable list of specific weapon attack speeds?

2 Likes

Because the tables are used as metatables, and will not be the final table, because they are modified later

2 Likes

I won’t lie to you, it looks really messy and hard to read. Consider using OOP and creating weapon classes.

4 Likes

i am actually using OOP, this is a modulescript containing all weapon stats

3 Likes

You can’t get attack_speed without referencing the whole system. You would need to do ItemSettings["BasicSword"][attack_speed]

3 Likes

really? doesn’t look particularly traditional…
I think just having weapon classes stored to each player as a standalone would be far neater, but each to their own.

2 Likes

that’s what i’m actually doing, im just using “ItemSettings” for the BASE weapon settings

3 Likes

you could save yourself a lot of hassle if you just put int values and string values into the item itself it seems like you are over complicating this a bit

4 Likes

ValueObjects are remarkably slower than using tables. However, OP’s way of laying out tables is specifically what needs work.

1 Like

pros out weigh the cons i.m.o yes tables are faster BUT

  • making changes to item values especially in games with lots of items is wayy easier if you just store them all in instance values in a folder in the tool rather than a convoluted table inside individual scripts

  • the speed difference is basically nothing because you just call the values once on instantiation of the script and its there as long as its running no recalling.

  • this way allows you to have one script that can be modular rather than a script for each item so if u want to make a drastic change to the script itself u wont have to update every single script in every single item

theres others but thats the mains

2 Likes

In large scale games, using metatables and OOP is one of the easiest and most friendly ways to lay out code. It’s a really, long, convoluted and detailed story, so you could probably do your own research on it. It’s best to use functional for miscellaneous things and OOP for more core mechanics of the game.

1 Like

yes but do you think this guy who cant handle a normal table is going to jump to that?? for barely any performace difference??? no. im catering to the users skill level.

1 Like

Their way of laying out code is not implicative of their skill level. While their means of laying out tables is poor, they have a solid understand of how tables work. If they know how to use OOP, which I am assuming they do since they said they are using it, there isn’t really a problem. It’s not about performance differences, its more about how to structure code in a more friendly manner.

1 Like

Unfortunately self referencing tables does not exist in Lua as the values you’re setting in the table don’t exist till after the line/indexing has finished.

local item_stats = {}

-- // Ignore this name thing
local item_name = "Basic Sword"
local list = {}

-- // Since the value of attack_speed has to be indexed before self reference
list.attack_speed = some_number
list.primary_function = {
    sequence = {
         [1] = {anim_speed = 1 * list.attack_speed}
    }
}

item_stats[item_name] = list

This is one of the only other ways I know of doing it without making it convoluted

3 Likes

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