Comparing two number attributes causing error?

I want to create it where stamina increases over time, but not go over a limit.
The problem is, when I run the code, it constantly gives a attempt to compare number <= boolean when both attributes are numbers. I have tried changing the code and double-checking that the attributes are numbers, and they are.

print("Stamina: "..Stamina)
	print("MaxStamina: "..MaxStamina)

	if not Character:FindFirstChild("InCombat") and not StaminaRegenDB and not Stamina >= MaxStamina then -- line with error
		Character:SetAttribute("Stamina", Character:GetAttribute("Stamina")+0.1)
		StaminaRegenDB = true
		
		delay(0.1,function()
			StaminaRegenDB = false
		end)

	elseif Character:FindFirstChild("InCombat") and not StaminaRegenDB and not Stamina >= MaxStamina then
		Character:SetAttribute("Stamina", Character:GetAttribute("Stamina")+0.1)
		StaminaRegenDB = true
		
		delay(0.5,function()
			StaminaRegenDB = false
		end)

	end

This is the exact error it is giving:
Workspace.shadowcrafter2017.CharacterManager:49: attempt to compare number <= boolean - Server
Stack Begin - Studio
Script ‘Workspace.shadowcrafter2017.CharacterManager’, Line 49 - Studio
Stack End - Studio

Thank you in advance!

It’s attempting to compare a number to a boolean because of the way your logic statement is written. It’s doing not Stamina, which returns the opposite of whether or not the stamina variable is defined, then checking if that value is less than a number. Putting the Stamina >= MaxStamina in parenthesis should fix it

Alternatively you could rewrite that part as just and Stamina < MaxStamina

2 Likes

Adding onto what Vari said, lua handles boolean values like this:

  • The value of false evaluates to false
  • The value of nil evaluates to false
  • Every other value evaluates to true

So by putting not Stamina what you’re doing is checking if stamina is nil or false (which is a boolean)

1 Like

Thanks, this was helpful as well. I recently started trying to make my code neater and shorter by using not on bools, but I never really understood what it meant or if it changed anything.

1 Like