MaxHealth Value Armor Problem

I’m using this armor system which is supposed to do this local “protect” command set to a certain value that reduces damage. That didn’t really work, so I supplanted it with some code that changes your max health and health accordingly. For example, if I put on a helmet, it increases my max health to 110, and adds 10+ health too. But if I put on an armored vest, it changes my max health to 120. I’m want both of the max health values to add up, instead of changing it to the last armor equipment that is put on. Examples of my code:

local h = tool.Parent:FindFirstChild("Humanoid")
	if (h ~= nil) then
		h.MaxHealth = 105
		h.Health = h.Health + 5

local h = tool.Parent:FindFirstChild(“Humanoid”)

h.MaxHealth = 105

	h.Health = h.Health + 5

If you don’t have too many types of armor (e.g. just head, torso, legs) what you could do is simply have a few variables that let you know what the bonus is in each slot. Then when you make a change your script could simply change the appropriate bonus value and add all the variables together with a base HP value and you’d get your new max HP.

Here’s some example code of what I’m talking about

local BASE_HP = 100
local hatBonus = 10
local shirtBonus = 20
local pantsBonus = 0

character.Humanoid.MaxHealth = BASE_HP + hatBonus + shirtBonus + pantsBonus

This would even allow for some items to lower your max HP if you needed it simply by using a negative bonus number.

I’m not using a custom inventory system to do all the armor things, I’m using a tool, so I don’t think this will help me that much.

In that case might it work to do something like this?

local bonus = 15

h.MaxHealth = h.MaxHealth + bonus
h.Health = h.Health + bonus

Your other alternative might be to have an intValue inside your armor pieces and name it something like HPBonus and then when you equip armor the tool simply searches your character model for items with that value name and then adds all of their value together with a base value to get your new MaxHealth.

EDIT:
For those curious, here’s an example code snippet of what the alternative might look like

local char = player.Character
local BASE_HP = 100
local bonus = 0

function searchObj(obj)
	for i, v in pairs(obj:GetChildren()) do
		if v.Name == "HPBonus" then
			bonus = bonus + v.Value -- v in an intValue or numberValue
		end
		searchObj(v)
	end
end

-- start with 0 bonus
bonus = 0
-- search character for bonuses and add them up
searchObj(char)

-- get change by calculating new MaxHP and subtracting current MaxHP
local hpChange = BASE_HP + bonus - char.Humanoid.MaxHealth

char.Humanoid.MaxHealth = BASE_HP + bonus
char.Humanoid.Health = char.Humanoid.Health + hpChange
-- Health simply adds the change to take into account situations
--  where the player isn't on full health
1 Like

thanks. that might probably work.

1 Like

I implemented it, adding the local declarative, and then adding the h.Health lines of code and replaced the old one. When I tried it out, it didn’t work. It seemed like it decreased my health instead of increasing it. I set the bonus value to 100, and the damage part is supposed to do 50. Normally, it would take three touches to kill me, but with armor it should take 5 hits. But that didn’t happen. Here a video showing:
WHY WONT YOU WORK - YouTube

Hmm, are you able to post the full code? It’s hard to tell what’s going on without looking at what you’ve got.

ok.
ServerMain:
local tool=script.Parent
local handle=tool.Handle
local h = tool.Parent:FindFirstChild(“Humanoid”)
local remotefunction=tool.RemoteFunction
local debris=game:GetService(“Debris”)

function remotefunction.OnServerInvoke(player,command,value)
if command==“protect” then
if value[100] then
local currentvest=value[1]:FindFirstChild(“VestArmour”)

		end
		local vest=Instance.new("Hat")
		vest.Name="VestArmour"
		handle:Clone().Parent=vest
		vest.AttachmentPos=Vector3.new(0, 1.75, 0.1)
	vest.Parent=value[1]
	

		
		tool.Handle.Transparency=1
		wait(2)
	tool.Handle.Transparency=0

local bonus = 100

	h.MaxHealth = h.MaxHealth + bonus

h.Health = h.Health + 100

	tool:Destroy()
	end
end

local player=game.Players.LocalPlayer
local character=player.Character
local humanoid=character.Humanoid
local bonus = 100
local tool=script.Parent
local handle=tool.Handle
local h = tool.Parent:FindFirstChild(“Humanoid”)
local event=tool:WaitForChild(“RemoteFunction”)
local protection= 1000

local ready=true

local debris=game:GetService(“Debris”)
local specialanim=humanoid:LoadAnimation(tool.special)

tool.Activated:connect(function()
local hum=character:FindFirstChild(“Humanoid”)
if hum and ready then
ready=false
specialanim:Play()

	h.MaxHealth = h.MaxHealth + bonus
	h.Health = h.Health + 100
	
	event:InvokeServer("protect",{character,hum,protection})
	ready=true
end

end)

so what’s the problem? this code is a free model, for the most part

Alrighty, so I noticed that the script would wait 2 seconds before adding the health which might have something to do with it acting weird. I also noticed there seemed to be a few unneeded parts of the scripts and some slightly outdated stuff so I went ahead and rewrote each script to try and make it cleaner and hopefully easier to read. I’m not sure if it’ll fix the issue but it’s worth a shot.

Also, as a side note here’s how to properly format code when you’re putting it on here :slight_smile:
DiscordCodeFormattingExample

LocalScript:

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character
local humanoid = character.Humanoid

local tool = script.Parent
local handle = tool.Handle
local event = tool.RemoteFunction

local equipping = false

local equipAnim = humanoid:FindFirstChildOfClass("Animator"):LoadAnimation(tool.special)

tool.Activated:Connect(function()
	if not equipping then
		equipping = true
		equipAnim:Play()
		event:InvokeServer()
	end
end)

ServerScript:

local tool = script.Parent
local handle = tool.Handle
local event = tool.RemoteFunction
local bonus = 100

event.OnServerInvoke:Connect(function(player)
	if player.Character == tool.Parent and not player.Character:FindFirstChild("VestArmor") then
		local character = player.Character
		local humanoid = character.Humanoid
		
		local vest = Instance.new("Hat")
		vest.Name = "VestArmour"
		local vestObj = handle:Clone()
		vestObj.Parent = vest
		vest.AttachmentPos = Vector3.new(0, 1.75, 0.1)
		vest.Parent = character
		
		handle.Transparency = 1
		
		humanoid.MaxHealth = humanoid.MaxHealth + bonus
		humanoid.Health = humanoid.Health + bonus
		
		wait(2)
		tool:Destroy()
	end
end)

it didn’t really work. I’ll screw around with some values and names, and maybe something might work

1 Like