Armor System Not Working

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?
    I want to make an armor system where the armor has a hp of 250 and will negate 30% of all damage the player takes. If the armor’s health is 0, then the negation of 30% goes away and the player will take full damage.

  2. What is the issue?
    Every time I play the game, the armor hp goes all the way down to 0 immediately. Also, before I tested the armor hp, the negation also doesn’t work.
    This proves the armor does take damage, but I don’t know what from.

  3. What solutions have you tried so far?
    I’ve tried to make basically the same topic, but my code wasn’t updated to what I have now.
    (I’m a little slow when it comes to learning, just a heads up)

Local Script in StarterCharacterScripts

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

local dmgHolder = 0

local debounce = false
local armor = workspace.ArmorEquip.Value --Duplicate script with bool value and increase negate by 0.1 everytime a class goes up
local negate = 0.3

local function reduceDamage(Damage)
	if debounce then
		debounce = false
		return
	end
	if armor == true then 
		
		debounce = true
		dmgHolder = (Damage-(Damage*negate))
		
		health -= dmgHolder
	end
end

humanoid.HealthChanged:Connect(function(Damage)	
	reduceDamage(Damage)
end)

Local Script in StarterGui

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 = script.Parent.armorHP
armorHP:SetAttribute("Health", 250)

local function detectDamage(humanoid, Damage)
	if equipped == true then
		
		script.Parent.Parent.Visible = true
		local atriArmor = armorHP:GetAttribute("Health")
			
		atriArmor-=Damage --Decreases HP of Armor
		damageHolder = Damage --Holds in Damage from takeDamage
		print("Oh no the Armor Took DAMAGE!!!1!11!")
		
		script.Parent.Size = UDim2.new(Damage / atriArmor, 0, 1, 0)
			
	else
		print("THE HUMAN IS TAKING DAMAGE")
		
		equipped = false
		script.Parent.Parent.Visible = false
		
		humanoid:TakeDamage(Damage)
		
	end
end

detectDamage(Humanoid, damageHolder

Are you sure you set this attribute correctly?
Try printing it. See what it gives you.

Edit: If it gives you a value try disabling the local script in StarterCharacterScripts. See if the value is still set.

One suggestion when fixing stuff is to try and debug where the problem happens. This could be done by debugging, disabling scripts, etc.

Oh, thanks for the debugging reminder. I keep forgetting I should print variables, anyways I set the health as 250 in the attribute, and the attribute it an IntValue. Then I got 250
image

Try disabling any scripts that are damage related.

Oh so the problem with that is, I have scripts all over the workspace that do damage. Most of them have the Humanoid:TakeDamage(num) The others have hum.Health = 0 If they get touched by a humanoid.

So disable those and see if it fixes the problem. Are you sure that when you spawn in your not touching anything related with the TakeDamage?

Yes, All the player is touching at the start is a part and a handgun I made. I’ll try to disable all the scripts as well. But as far as I know the ones that do Humanoid:TakeDamage(num) only activates when they are touching the player

Yeah, consider disabling or moving it away from the player. And see if your armour is at full health.

So I deleted all the npcs that have the damage script and saved it in another baseplate. Then when I played the game, it gave the same output

Does this show the full HP when you removed them?

Theres a Gui I made for showing the health, that goes all the way down and disappears when I play the game. However, the atriArmor had a health of 250 still, which is full health. I’m sorry I didn’t clarify enough. This was also before I did the print debug for atriArmor

Sorry for the long wait. Could you share the health GUI script then if that’s the problem?

I’m sorry for the long wait as well, I was really busy yesterday.
But the day before yesterday, I changed my code a bit and a friend helped me with this. So whenever I put in a number for the damage holder, the armor will take damage. But when I get hit by an enemy, the armor doesn’t take damage.
Here’s the script. It’s also a local script in StarterGui as well.

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 = script.Parent.armorHP
armorHP:SetAttribute("Health", 250)
armorHP:SetAttribute("MaxHealth", 250)


local function detectDamage(humanoid, Damage)
	if equipped == true then

		script.Parent.Parent.Visible = true
		local atriArmor = armorHP:GetAttribute("Health")
		local MaxatriArmor = armorHP:GetAttribute("MaxHealth")
		local healthBar = atriArmor/MaxatriArmor
		print(armorHP:GetAttribute("Health"))

		armorHP:SetAttribute("Health",atriArmor - Damage) --Decreases HP of Armor

		print(armorHP:GetAttribute("Health"))
		damageHolder = Damage --Holds in Damage from takeDamage
		print("Oh no the Armor Took DAMAGE!!!1!11!")

		script.Parent.Size = UDim2.new(healthBar, 0, 1, 0)
	else
		print("THE HUMAN IS TAKING DAMAGE")

		equipped = false
		script.Parent.Parent.Visible = false

		humanoid:TakeDamage(Damage)
	end
end

detectDamage(Humanoid, damageHolder)

Suggest moving this:

to after this line:

It also seems like you did it right. But just to make sure, check the size of the GUI health in the properties when you run the game to see what progress has been done. I also suggest debugging the damage amount. (Print it).

I moved the lines to where you’ve told me to, and it says there is an unknown global ‘atriArmor’

	if equipped == true then

		script.Parent.Parent.Visible = true
		
		print(armorHP:GetAttribute("Health"))

		armorHP:SetAttribute("Health",atriArmor - Damage) --Decreases HP of Armor
		local atriArmor = armorHP:GetAttribute("Health")
		local MaxatriArmor = armorHP:GetAttribute("MaxHealth")
		local healthBar = atriArmor/MaxatriArmor

		print(armorHP:GetAttribute("Health"))
		damageHolder = Damage --Holds in Damage from takeDamage
		print("Oh no the Armor Took DAMAGE!!!1!11!")

		script.Parent.Size = UDim2.new(healthBar, 0, 1, 0)
	else
		print("THE HUMAN IS TAKING DAMAGE")

		equipped = false
		script.Parent.Parent.Visible = false

		humanoid:TakeDamage(Damage)
	end
end

It also printed out the damage and it goes from 250 to 225, if I put 25 in damage holder and it says the armor took damage.

If you were to do this you would need to make it this

armorHP:SetAttribute(“Health”,armorHP:
Getattribute (“Health”) - Damage)

Can I see how the enemy damages the player?

The enemy damages the player like this

function attack(target)
	if (myRoot.Position - target.Position).magnitude < 5 then
		grabAnim:Play()
		grabSound:Play()
		if target.Parent ~= nil then
			if playerArmor.Value == true then
				target.Parent.Humanoid:TakeDamage(10)
			else
				target.Parent.Humanoid:TakeDamage(25)
			end
		end
		wait(1)
	end
end