Hello guys,
I need to attach shield to player. I know how to do that but I have one problem. Shield should have own HP and only if the shield is hitted it should take damage. So basically player and shield should have two different HPs. But when I insert shield with humanoid into chatacter and connect it with Welder Parts it is working weirdly. If the shield is “killed” instead of disappearing it is just killing the player who owns it.
Can you show it?
Also, why don’t you make a fake hp to the shield?
An int value that works like the hp, and make the shield disappear when it reaches 0 instead of using a humanoid
Can’t show, not on PC currently.
And idk how to make my gun and sword damage it with using int because for now it only detects humanoid
Do so it detects if has a humanoid and a shield
if both, decrease the int value to the damage ammount
if just humanoid, decrease the health of the humanoid
You could check for the IntValue by using FindFirstChild or tag the shield with CollectionService.
If you’re using Touched for your weapons, your code would look something like this:
weapon.Touched:Connect(function(hitPart)
-- Damaging characters
local model = hitPart:FindFirstAncestorWhichIsA("Model")
local humanoid
if model then
humanoid = characterModel:FindFirstChild("Humanoid")
end
if humanoid then
humanoid:TakeDamage(damage)
return
end
-- Checks if what we've hit is a shield; adjust accordingly if your shield is a model and you parented your IntValue somewhere else
local shieldHealth = hitPart:FindFirstChild("Health")
if shieldHealth then
shieldHealth.Value -= damage
end
end)
CollectionService alternative:
You first have to tag the shield when it’s created, so put CollectionService:AddTag(shield, "Shield")
wherever that’s being done.
Then, to check, use CollectionService:HasTag(part, "Shield")
in an if statement; if this is true, you know you’ve hit the shield and can damage it.
Super EZ (IM Not being toxic btw)
To create a shield with its own HP that takes damage separately from the player’s HP, you need to manage the damage separately for the player and the shield. You can can do this by setting up a custom script for the shield that handles the damage and destruction.
Here’s a general outline of how you can approach this:
-
Create the shield model and insert it as a child of the player’s character. Make sure the shield has a separate part for its visual representation and another part for the “hitbox” or “collision” where it will receive damage.
-
Add a custom script inside the shield model that manages its HP and damage-taking logic.
This is an example of how the script will and can be plus you can modify the scwipt to ur liking:
local shield = script.Parent
local shieldHealth = 100 -- Set the initial shield HP here
function onHit(damageAmount)
shieldHealth = shieldHealth - damageAmount
if shieldHealth <= 0 then
-- Shield is destroyed, so we remove it from the character
shield:Destroy()
end
end
function onTouched(hitPart)
local humanoid = hitPart.Parent:FindFirstChild("Humanoid")
if humanoid and shieldHealth > 0 then
-- Apply damage to the shield if it's still active
onHit(10) -- You can adjust the damage amount as needed
end
end
shield.Touched:Connect(onTouched)
btw if u don’t understand even with the comments you can read Explanations
Explanation
So In this example, we assume that the shield starts with 100 HP (shieldHealth = 100
). When the shield gets hit (Touched
event is triggered), it checks if it still has HP (shieldHealth > 0
). If so, it takes damage by calling the onHit
function. You can adjust the damage amount (here set as 10) as needed.
If the shield’s HP drops to 0 or below, it gets destroyed using shield:Destroy()
.
Make sure the script is placed inside the shield model, and the shield model is inserted as a child of the player’s character. When the player is hit, it will damage the player’s default Humanoid, and when the shield is hit, it will take damage independently without affecting the player’s HP.
I hope this helps a lot MY G
This should be handled by hit registry function you are using. Have it detect if a player has a shield and if the shield has enough HP (that should be an integer value, ideally in an instance). If the hit’s damage does more than shield’s health have it destroy the instance and then you can optionally apply the leftover damage to player’s health if you want.
Thanks! That’s exactly what I needed, just couldn’t thought of a way to do.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.