My OOP damage acts odd?

Feel like I’m a beginner all over again. As I do my OOP combat, after using a method someone told me in the previous summary, I seem to struggle with getting a working damage interpretation system going. When I use this function

local DamageInterpretation = {}
DamageInterpretation.__index = DamageInterpretation

local AvailableProperties = {
	BlockBreak = "boolean",
	WeaponType = "string",
	KnockBack = "boolean",
	Blockable = "boolean",
	AttackType = "string",
	Damage = "number",
	KBTime = "number",
	QiType = "string",
}

function DamageInterpretation.new(properties: {})
	return setmetatable({}, DamageInterpretation):_Initialize(properties)
end

function DamageInterpretation:_Initialize(properties: {})
	for key, value in pairs(properties) do
		local dataType = AvailableProperties[key]

		--[[
			This step is unnecessary all you need is `self[key] = value`,
			this just checks if the type of data is correct/the same and
			the key should be added.
		]]
		if dataType then
			if typeof(value) == dataType then
				self[key] = value
				
			else
				error(("%s must be a type of %s"):format(key, dataType))
			end
		else
			warn("%s can't be added")
		end
	end

	return self
end

The script above is how I attempt to setup the scripts, but then I end up having an issue.

image
This issue is the result of attempting to use this function

local function damageHum(hum,dmg)
		--	if hitchar.Name == "PuppetModel" then return end
		local minHp = 0.3

		print(hum)
		print(dmg)

		if hum.Health ~= minHp then
			hum.Health = math.clamp(hum.Health - dmg,minHp,math.max(hum.MaxHealth,.15))
		else
			hum.Health = minHp + 0.3
			game:GetService("RunService").Heartbeat:Wait()
			hum.Health = minHp
		end
	end

The thing I tried to solve earlier was to print self, instead began to tell me the issue which said
image

I was attempting to figure out why damage is now a function (because only now it’s doing that, earlier it was saying that self was nil)

What is the last thing to change the dmg value before it is put into the function?

Is there a reason you can’t have the damageHum function as a class method of DamageInterpretation? If so, can you show how you’re calling damageHum?

I attempted to change the values through my combat module script.


	local AvailableProperties = {
		BlockBreak = true,
		WeaponType = "H2H",
		KnockBack = false,
		Blockable = true,
		AttackType = "Blunt",
		Damage = 4,
	}
	
	DamageInterpretation.new(AvailableProperties)

	local Character = Player.Character
	
	local HitResult, EnemyChar = Presets.HandToHandHitbox(Player)
	
	Utilities:AddValues(Character.StatusFolder, "BoolValue", "SwingStun", Length * 1.5)
	
	if HitResult == true then
		

		

	DamageInterpretation:Damage(Character, EnemyChar.Humanoid, EnemyChar) -- Right here.
		

Besides the fact I don’t see the usage of the damageHum script outside of that module, there’s no other reason its not another method.

function DamageInterpretation:Damage(Character, HitHum, HitChar)
	print(self)
	
	local DMG = self.Damage
	local OGDMG = DMG
	
	
	if not HitChar then return end
	if HitChar:FindFirstChildOfClass("ForceField") then return end
	if Character:FindFirstChildOfClass("ForceField") then return end
	
	local function damageHum(hum,dmg)
		--	if hitchar.Name == "PuppetModel" then return end
		local minHp = 0.3

		print(hum)
		print(dmg)

		if hum.Health ~= minHp then
			hum.Health = math.clamp(hum.Health - dmg,minHp,math.max(hum.MaxHealth,.15))
		else
			hum.Health = minHp + 0.3
			game:GetService("RunService").Heartbeat:Wait()
			hum.Health = minHp
		end
	end
	
	if self.KnockBack == true then
		local BV = Instance.new("BodyVelocity")
		BV.Parent = HitChar:FindFirstChild("HumanoidRootPart")
		BV.MaxForce = Vector3.new(100000, 22500, 100000)
		BV.P = 33300
		BV.Velocity = Character.HumanoidRootPart.CFrame.LookVector * 40
		Debris:AddItem(BV, self.KBTime)
	end
	
	local Player = Players:GetPlayerFromCharacter(Character)
	local ePlayer = Players:GetPlayerFromCharacter(HitChar)
	
	if self.Blockable == true and self.AttackType == "Blunt" then
		DMG = DMG/2
	end
	
	damageHum(HitHum, DMG)
	
end

Sorry for not immediately answering.

I see, how are you defining HitHum?

Through a seperate hitbox module

That’s probably where the stems from then, is that function returning a function?

Nope, the function is giving me what it’s suppose to which is the hitcharacter and if the actual hitresult is true.

Actually I think the issue is here, you’re calling DamageInterpretation.new(AvailableProperties) but you’re never assigning it to a variable so when you go to call its methods you’re actually passing the DamageInterpretation module as self, not the DamageInterpretation object as self.

1 Like

You were correct tysm! I just have another issue sadly that the things being put into properties isn’t being fulfilled, but that’s for another time.

1 Like

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