How do i make "Shield Health?"

I want to make this kind of system where you can get “Shield” Health. Its like normal health, but it cant regen and kind of stays the same.

for example: if you got 10 shield health and got damaged for 5 hp, you would have 5 shield health and it would not regen. But the thing is, i dont want this to really be associated with health (no just increasing max hp or health.)
if you know a way that would be greatly appreciated

1 Like

You can create a data store automatically assigning 10 “shield health” to each player when they join and only when this datastore value hits 0 does their character health get affected.

2 Likes

An easy way to do it is just have a table that stores the shield data for Players. Then when they take damage, you would take hp from that first before you go into their player health.

4 Likes

A datastore isn’t really needed for this. That would take away from the overall datastore calls you can make, and you can do it with a normal table.

1 Like

Tables don’t save data and from this …

It sounds like it would be a one time thing so the table would just reset this every time the player rejoins.

1 Like

If they wanted it to save they would just save it when the player leaves and read their shield level when they rejoin, you wouldn’t want to constantly read and write to the datastore though during run time.

But nothing in what they said made it sound like they wanted it to save.

Regardless, you would only use the datastore to save it when the player leaves and joins, then use a table in game to store all the current shield data.

1 Like

could you guys give me a coding example of this please? thanks!

“Does not regen” indicates that it should save, hence datastores. The implementation will depend on what OP wants done with it.

Do you want it to save or not? Were gonna need that clarified.

Its actually both. it might be used more if it is saved, but otherwise both is okay.

@ChickHenEn For the coding example let’s take the example of a normal linked sword, This is the damage function of the sword executed on Touched event.

Linked sword damage function, see toolbox provided by ROBLOX
function Blow(Hit)
	if not Hit or not Hit.Parent or not CheckIfAlive() or not ToolEquipped then
		return
	end
	local RightArm = Character:FindFirstChild("Right Arm") or Character:FindFirstChild("RightHand")
	if not RightArm then
		return
	end
	local RightGrip = RightArm:FindFirstChild("RightGrip")
	if not RightGrip or (RightGrip.Part0 ~= Handle and RightGrip.Part1 ~= Handle) then
		return
	end
	local character = Hit.Parent
	if character == Character then
		return
	end
	local humanoid = character:FindFirstChildOfClass("Humanoid")
	if not humanoid or humanoid.Health == 0 then
		return
	end
	local player = Players:GetPlayerFromCharacter(character)
	if player and (player == Player or IsTeamMate(Player, player)) then
		return
	end
	UntagHumanoid(humanoid)
	TagHumanoid(humanoid, Player)
	humanoid:TakeDamage(Damage)	
end

The key is here the humanoid take damage, and the issue is that it only has a health system which we want to seperate from a shield system:

	humanoid:TakeDamage(Damage)	

Consequently, to seperate the two the only way is to create a new take damage function that will consider both the humanoid health and the shield system we have as we cannot modify the build in humanoid functions directly. I also propose using attributes as we can directly store this value inside a humanoid for easy access, just like the fact we can use humanoid.Health to easily access health.

Psuedo code DON"T COPY AND PASTE, I’ll leave it as an exercise for you in order to avoid giving out code for free.

local function customHumanoidTakeDamage(humanoid,Damage)
	local shieldAmount = humanoid:GetAttribute("Shield") --obtain the data for this specific character this specific humanoid that it has a shield
	if there is a shield then
		decrease the shield attribute -- damage the shield
	elseif there is no shield then
		humanoid:TakeDamage(Damage)	-- use the default humanoid health decrease system
	end
end

Then you can just do this:

customHumanoidTakeDamage(humanoid,Damage)
4 Likes

Thanks for not giving the code away!

1 Like

sorry if this is a bit late but i dont think im not fully understanding this code. Im incredibly new to roblox coding so you might need to explain in a simpler way

local function customHumanoidTakeDamage(humanoid,Damage)
	local shieldAmount = humanoid:GetAttribute("Shield") --obtain the data for this specific character this specific humanoid that it has a shield
	if shieldAmount then
		shieldAmount = shieldAmount - 1 -- damage the shield
	else
		
		humanoid:TakeDamage(Damage)	-- use the default humanoid health decrease system
	end
end

this is the code I have at the moment. Could you give me a hint on whats wrong?

oops i formatted wrongly. ignore the first text and code and go to the second

The issue with the code is that this line

Only modifiies the local variable shield amount, not the attribute within the humanoid.

You will need to use :Set attribute in order to update the value within the humanoid which the code is checking.

alright thanks a lot!
another question i have is
can the script be in any location? or does it have to be in a specific one

This line of code will need to replace the damage logic within the weapon script.

im getting this error for some reason

When you get an error you will need to click on it to go to the script at that specific line and identify what the code is doing at this line.

how would i set “shield health” to like 5?

Im sorry if im asking for a bit too much, but can you show me an example of something like a damage part using this code?