Metatables, memory leaks, and multiple repetitions?

Trying to learn more about OOP and metatables and started running into issues I don’t understand.

My output window shows that the initial setup fires twice?

I have print functions set to test when debounce gets set back to false allowing another attack to go through. Whenever a combo resets back to 1, the debounce print fires equal to how many rotations have passed?

This is all currently clientsided

image

local script

local Auxillary = require(game:GetService("ReplicatedStorage").Modules.Auxillary.Auxillary)
local Services = Auxillary.Services

local Player = Services.p.LocalPlayer

local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

local testRequire = require(game:GetService("ReplicatedStorage").Modules.CombatSystem.Sword.Skillset)
Humanoid:SetStateEnabled(Enum.HumanoidStateType.FallingDown, false)

local Setup = testRequire.Setup(Player)

function InputHandler(ActionName, InputState, InputObject)
	
	if ActionName == "Attack" then
		if InputState == Enum.UserInputState.Begin then
			Setup:Attack()
		end
	end
	

	
	return Enum.ContextActionResult.Pass

end

Services.cas:BindAction("Attack", InputHandler, true, Enum.UserInputType.MouseButton1,  Enum.KeyCode.ButtonA)

module script

function SwordSkillset:Attack()
	
	if self.Debounce then return end

	self.Debounce = true
	
	local Character = self.User.Character
	
	-- If on ground, play animation.
	if Character.Humanoid.FloorMaterial ~= Enum.Material.Air then
		self.Animations["Attack_Ground_".. self.Combo]:Play(0)
		
		self.Animations["Attack_Ground_".. self.Combo].Stopped:Connect(function()
			if not self.Debounce then return end
			self.Debounce = false
			print("debounce false")
		end)

	end
	
	-- Testing for now. When working, this will only fire when the attack connects with an enemy
	if self.Debounce then
		-- If attack is not a flourish and your attack hits the enemy, cancel debounce early.
		if not Services.cs:HasTag(Assets.Animations["Attack_Ground_".. self.Combo], "Flourish") then 
			--
			self.Animations["Attack_Ground_".. self.Combo]:GetMarkerReachedSignal("DebounceEnd"):Connect(function()
				self.Debounce = false
				print("debounce false")
			end)

		end
		
	end
	
	-- Increase combo by 1. If flourish, reset combo.
	if self.Combo == 3 then
		print("combo complete, reset back to 1")
		self.Combo = 1
	else
		self.Combo += 1
	end
	
end

Hola, I can’t tell you why it’s printing multiple times on start but what I can tell you is that it’s printing more and more is because of the connections that are still active when you call :attack()

Each time you call :Attack(), this connection, connects, but it doesn’t go away meaning it will keep being called. Creating multiple connections overtime
To get rid of the connections you would have to do something like
connection:disconnect()
However you want to implement that.

1 Like

Ahhh gotcha. Tysm this fixed it for me. I’ll figure out that double print some other time it’s not too much of a bother right now. Thank you again though!

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