How to make Armor/Health System

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

  1. What do you want to achieve? Keep it simple and clear!
    I’m trying to make an Armor and Health system and I wanna know how I would make it so if a player has armor then it will damage the armor value but if the armor is below 0 then it will damage the health. For example, is if I have 30 Armor and 100 health and we damage the armor by 40 we would have -10 armor and I wanna take away health by that amount.

Server Script:

local Main = require(script.Parent.MainModule)
local RE_Health = game.ReplicatedStorage.Health -- Health RemoteEvent

-- Assigns Health/Armor Values to players.
game:GetService("Players").PlayerAdded:Connect(function(plr)
	local Health = Instance.new("IntValue")
	Health.Name = "Health"
	Health.Parent = plr
	Health.Value = 100
	local Armor = Instance.new("IntValue")
	Armor.Name = "Armor"
	Armor.Parent = plr
end)

RE_Health.OnServerEvent:Connect(function(plr, Health, Armor)
	Main.DamagePlayer(plr, 40)
end)

Local Script:

local RE_Health = game.ReplicatedStorage.Health
local plr = game.Players.LocalPlayer
local Health = plr.Health
local Armor = plr.Armor
local Healthbar = script.Parent.Background.Healthbar
local Armorbar = script.Parent.Background.Armorbar

Health.Changed:Connect(function() -- Detects if health value has changed
	Healthbar:TweenSize(UDim2.fromScale(Health.Value/100, 1), Enum.EasingDirection.Out, Enum.EasingStyle.Quad, 0.3, true)
	RE_Health:FireServer(Health.Value, Armor.Value) -- Gives the server the health and armor values
end)

Armor.Changed:Connect(function() -- Detects if armor value has changed
	Armorbar:TweenSize(UDim2.fromScale(Armor.Value/100, 0.5), Enum.EasingDirection.Out, Enum.EasingStyle.Quad, 0.3, true)
	RE_Health:FireServer(Health.Value, Armor.Value) -- Gives the server the health and armor values
end)

Main Module Script:

local module = {}

function module.DamagePlayer(plr, Damage)
	if plr.Armor.Value >= 0 then
		plr.Armor.Value -= Damage
		if plr.Armor.Value < 0 then
			local LeftOver = plr.Armor.Value + plr.Health.Value
			plr.Health.Value = LeftOver
			plr.Armor.Value = 0
		end
	else
		plr.Health.Value -= Damage
	end
end

return module

Feel free to point out any mistakes I have made

4 Likes

This seems like a super unsecure way to create an armor and health system. The client should NEVER give the server data it relies on to make decisions like damage.

1 Like