Health gui taking damage when part is touched by npc

Hello, I’ve been learning to script the past few weeks and managed to script a little game. I cannot find anything on how to start scripting this health billboard gui to take damage when my npcs touch it’s part.
I’ve tried comissioning two scripters to help but neither of them knew how to.

any help to point me in the right direction would be brilliant

Screenshot 2022-07-29 214629

1 Like

could you send the name of the bill board and the way the stuff is layed out in the explorer?

1 Like

Create a script inside the part and paste this

local health = 100
script.Parent.Touched:Connect(function(hit)
    if hit.Parent.Name == "NPC" then
        health = health - 10
        if health <= 0 then
            script.Parent:Destroy()
        end
    end
end)

Use “health” variable to animate the health GUI, I don’t know how to animate GUI lol.
Everytimes a part got hit by an NPC, the health will be subtract by 10 if the health reach 0 and less than 0, the part will be destroyed

Screenshot 2022-08-01 125745

Why is there two framessssssssssss

I have one thats red and one thats green and I was trying to make the green decrease to reveal the red

Alright im almost readyyyyyyyyyyyy

Here, make a script inside the billboard gui, than paste this into it.


local minBox = script.Parent:WaitForChild("CurrentHealth")

local maxBox = script.Parent:WaitForChild("MaxHealth")

local boxSizeX = 200

local boxSizeY = 50

function update(percent)
     local greenXSize = boxSizeX*(percent/100)
     local redSize = math.max(0,200-greenXSize)
     local redPosOffset = greenXSize
     minBox.Size = UDim2.fromOffset(greenXSize,boxSizeY)
     maxBox.Size = UDim2.fromOffset(redSize,boxSizeY)
     maxBox.Position = UDim2.fromOffset(greenXSize,0)
end
1 Like

Thats just the script to make the bars move correctly.

1 Like

Put a ‘Humanoid’ instance inside the ‘Base’ model and decrease its health each time any of its children (parts) are touched by an NPC.

1 Like
local Script = script
local Base = Script.Parent --Reference to base.
local Humanoid = Base.Humanoid --The base's 'Humanoid'.
local HealthGui = Base.HealthGui
local CurrentHealthFrame = HealthGui.CurrentHealth

local function OnHealthChanged(Health)
	CurrentHealthFrame.Size = UDim2.new(0, 0, 0, 0) --You will need to change this according to the frame's current settings and the value of 'Health'.
end

local function OnTouched(Part) --May require a debounce, implement one if necessary.
	local Model = Part:FindFirstAncestorOfClass("Model")
	if not Model then return end
	if Model.Name ~= "Enemy" then return end --Check if the model is an enemy (you can use the 'CollectionService' or some other tagging method).
	Humanoid.Health -= 5 --Deal damage to the base.
end

Humanoid.HealthChanged:Connect(OnHealthChanged)
for _, Child in ipairs(Base:GetChildren()) do
	if Child:IsA("BasePart") then Child.Touched:Connect(OnTouched) end
end

Some pseudo-code to help steer you in the right direction.

1 Like

Thanks for this, i’ll take a good look at it