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
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
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
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.