OOP Table not working

I’m making a combat script and i’m gonna use oop for it but the issue is when i call self.Character or anything in the metatable it is nil. When i print self

[“Attack”] = “function”,
[“SetMetatable”] = “function”,
[“__index”] = ▼ {
[“Attack”] = “function”,
[“SetMetatable”] = “function”,
[“__index”] = “*** cycle table reference detected ***”
}

this is what shows up.

CombatEvent.OnServerEvent:Connect(function(Player, AttackType)
	local Combat = CombatManager.SetMetatable()
	Combat.Character = Player.Character
	Combat.AttackType = AttackType
	Combat.StopTime = .34
	Combat.StartTime = .08
	Combat.MaxCombo = 4
	CombatManager:Attack()
end)

local module = {}
module.__index = module

function module.SetMetatable()
	return setmetatable({
		AttackType = "m1",
		Damage = 0,
		HitboxSize = Vector3.new(6,6,6),
		StartTime = .08,
		StopTime = .34,
		Character = nil,
		MaxCombo = 4,
	}, module)
end

function module:Attack()
	local Character = self.Character
	local Humanoid = Character.Humanoid
	local RootPart = Humanoid.RootPart
	local Animator = Humanoid.Animator
2 Likes

Tell me, why do you need the module.SetMetatable() function if you can create a metable without it?

i use it to like set the settings of the combat attack depending on which weapon i use

Your issue could be that you set Character to nil in the script.

function module.SetMetatable()
	return setmetatable({
		AttackType = "m1",
		Damage = 0,
		HitboxSize = Vector3.new(6,6,6),
		StartTime = .08,
		StopTime = .34,
		Character = nil, -- You're setting self.Character to nil.
		MaxCombo = 4,
	}, module)
end

Yes but i believe it’s supposed to change once i set the metatable

Could you show some of the script that’s using the SetMetatable() function?

it’s the first script i showcased

CombatEvent.OnServerEvent:Connect(function(Player, AttackType)
	local Combat = CombatManager.SetMetatable()
	Combat.Character = Player.Character
	Combat.AttackType = AttackType
	Combat.StopTime = .34
	Combat.StartTime = .08
	Combat.MaxCombo = 4
	CombatManager:Attack()
end)

Most likely nil because you are accessing a metatable and not a regular table.

function module:Attack()
	local Character = self.Character
	local Humanoid = Character.Humanoid
	local RootPart = Humanoid.RootPart
	local Animator = Humanoid.Animator

Try to change following scripts:

function module.Attack(self)
	local Character = self.Character
	local Humanoid = Character.Humanoid
	local RootPart = Humanoid.RootPart
	local Animator = Humanoid.Animator
CombatEvent.OnServerEvent:Connect(function(Player, AttackType)
	local Combat = CombatManager.SetMetatable()
	Combat.Character = Player.Character
	Combat.AttackType = AttackType
	Combat.StopTime = .34
	Combat.StartTime = .08
	Combat.MaxCombo = 4
	CombatManager.Attack(Combat)
end)

You’re not calling Attack on the object you created.

You’re calling the method of the CombatManager class, not the object, try to use
Combat:Attack()

1 Like

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