Attempt to perform arithmetic (sub) on nil and number Error

  1. What do you want to achieve? Keep it simple and clear!
    This script is under StarterGui and whenever the player gets hit, the armor takes the damage. Then once the armor’s hp goes to 0 the armor is unequipped.

  2. What is the issue?
    Whenever I run the script I get this error, attempt to perform arithmetic (sub) on nil and number
    I also need help actually dividing the damage the player takes by 30%, but that can be figured out later.
    When I run the game this happens

  3. What solutions have you tried so far?
    I’ve tried searching online, but I can’t find a solution relating to my problems

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local Humanoid = character:WaitForChild("Humanoid")
local health = Humanoid.Health

local damageHolder = 0
local equipped = workspace.ArmorEquip.Value

local armorHP = 250

local function detectDamage(humanoid, Damage)
	local AtriArmor = humanoid:GetAttribute("armorHP")
	
	if equipped == true then
		
		script.Parent.Parent.Visible = true
		
			AtriArmor-=Damage --The line where the error is
			damageHolder = Damage 
			print("Oh no the Armor Took DAMAGE!!!1!11!")
		
		script.Parent.Size = UDim2.new(AtriArmor / Damage, 0, 1, 0)
			
	else
		print("THE HUMAN IS TAKING DAMAGE")
		AtriArmor = 0
		script.Parent.Parent.Visible = false
		equipped = false
		humanoid:TakeDamage(Damage)
		
	end
end

detectDamage(Humanoid, damageHolder)

Could you be more specific on what the error is? Maybe include a screenshot :slight_smile:

Thank you for feedback, I included a screenshot of the problem

Try using SetAttribute to change the Attribute

humanoid:SetAttribute("armorHP", humanoid:GetAttribute("armorHP") -= Damage)

This will still error if humanoid:GetAttribute("armorHP") is nil or doesn’t exist

So it looks like the attribute armorHP is returning a nil value. Can you check to make sure there is a value assigned to the attribute?

But at the start I set armorHP as 250? Shouldn’t it be an int value or something like that?

You set a variable, you didn’t set an attribute

How am I supposed to set an attribute?

I did say it here:

Text

Humanoid:SetAttribute("armorHP", 250)
1 Like

Right. Attribute is a special value associated with a part. At the beginning, instead of saying
armorHP = 250

Say
humanoid:SetAttribute("armorHP", 250)

I see, so this would basically make a separate hp bar for the armor?

It just depends on how you use it; so, yes

Ah, so it doesn’t output the error anymore but now the armor is taking damage for some reason. Did I use the local function incorrectly somehow?

I assume that you want the armor to only take damage when it’s hit, in that case, I recommend opening a new topic since that would be a little more complex

Not neccesarily

Ah ok, thank you for helping me and @Bloxlin_Dev and @DasKairo

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