Why is the print always returning 1?

Hello DevForum, I’m making a combat system by using OOP and MetaTables, but I came across an error:

The combo, which starts at 1, doesn’t increase per m1, it just stays at 1.

Here is my code:

function CombatModule.new(player : Player, weapon : StringValue)
	local self = setmetatable({}, CombatModule)
	self.Player = player
	self.Weapon = weapon
	
	self.ComboCooldown = 1
	self.Windup = .15
	self.M1Time = .3
	self.LastM1 = 0
	self.LastComboEnd = 0
	self.M1Cooldown = false
	
	self.Combo = 1
	self.MaxCombo = 5
	self.Damage = 5
	
	self.Animations = {}
	
	for i,v in Animations:GetChildren() do
		self.Animations[v.Name] = player.Character.Humanoid.Animator:LoadAnimation(v)
	end
	
	return self
end

function CombatModule.CanDoAction(character : Model)
	if character:GetAttribute("Stunned") == false and character:GetAttribute("Action") == nil then
		return true
	else
		return false
	end
end

function CombatModule:BeginCombat()
	local character = self.Player.Character
	local combo = self.Combo
	
	if character then
		if self.M1Cooldown == false and CombatModule.CanDoAction(character) then
			self.M1Cooldown = true
			
			if os.clock() - self.LastM1 >= 1 then
				combo = 1
			end
			
			self.LastM1 = os.clock()
			
			print(combo) -- This is where it returns 1
			
			if combo > self.MaxCombo then
				combo = 1
			else
				combo += 1
			end
			
			self.M1Cooldown = false
		end
	end
end

Feedback is appreciated!

Remove the “combo” variable and replace all mentions with self.Combo. You believe you’re creating a reference to this property, but Lua(u) does not allow this. Numbers are value-types, so the moment you assign a variable to one from another source, it’s copied. Any time you want to know or update the current state of self.Combo, you must read or write to the property directly

1 Like

Thank you so much! Never knew that was how it worked, I appreciate the reply!

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