How to make a healing station

I am trying to recreate the Rickey Rat healing station, I managed to make some kind of billboard gui but it is very broken and I don’t know how to fix it.

image

And here is the script:

local part = script.Parent
local prompt = part.ProximityPrompt

local sound = part.DingSound

MaxUses = 10

prompt.Triggered:Connect(function(player)
	local hum = player.Character.Humanoid
	
	hum.Health = hum.Health + 10
	MaxUses = MaxUses - 1
	
	sound:Play()
	
	if MaxUses == 0 then
		prompt.Enabled = false
		part.BrickColor = BrickColor.new("Really red")
	end
end)

So my problem is that the billboard guis properties are like scaled weirdly and probably is wrong. And I need to make it so when you use the healing station, the health bar goes down until the station runs out of uses. But I am not sure how to do that.

1 Like

Here is the model:
HealingStation.rbxm (9.6 KB)

i think we shall create a IntValue named “MaxUses”
in the Script, we check in prompt.Triggered whether MaxUses.Value > 0 so we can use it.
MaxUses.Value -= 1 to decrease it when it is used

then in HealthGui, we connect to its changed property

MaxUses.Changed:Connect(function(newValue: number)
  -- change health bar accordingly
end)

this way, we have better isolation between the value logic, and ui/apperance logic

2 Likes

Your current script doesn’t handle emptying the health bar. Just add that with a simple math calculation, and if you want you can tween it too:

local part = script.Parent
local prompt = part.ProximityPrompt

local sound = part.DingSound
local fill = --your foreground frame of the health bar

MaxUses = 10

prompt.Triggered:Connect(function(player)
	local hum = player.Character.Humanoid
	
        MaxUses -= 1
	hum.Health += 10
	
	sound:Play()
        fill.Size = Udim2.fromScale(MaxUses/10, 1)--divide by maximum usage (10) to get a percentage
	
	if MaxUses == 0 then
		prompt.Enabled = false
		part.BrickColor = BrickColor.new("Really red")
	end
end)

Looking at your hierarchy, fill in this case should be ‘health’.

Edit: if your billboard gui is scaling when the player gets further away and you don’t want that. Converts its Size property to scale:

Gui.Size = Udim2.fromScale(4, 1)--some scale value

Now it will no longer resize

2 Likes

It actually worked so well! Thanks so much

1 Like

But I got one question, how can I fix the red part from clipping under?

if we are using Udim2.fromScale(MaxUses/10, 1) , it means we want the inner bar height the same as its parent.
if we didn’t use a texture, like they are just the BackgroundColor green and red, the green should cover the same pixels and no red shall show through.

have you use a UIPadding that could change the final height of the bar?

1 Like

I fixed it, I just had to change the size property of the “Health” a bit bigger

2 Likes

I know this is a bit unrelated to this topic. But is there a way I can make the same but as an npc’s health? So it has the gui and when it takes damage, the bar goes down and when the bar is completely down the npc gets destroyed from workspace and plays a sound?

it should be the same. an NPC is also a model, so it’ll have its own BillboardGui for health bar,
its Script instead of the proximity prompt,
we connect to the npc’s humanoid’s health changed

local npc = script.Parent
npc.Humanoid.HealthChanged:Connect(function(newHealth: number)
  -- change the size of the health bar
end)

Would it be something like this?

local npc = script.Parent

local sound = npc.Torso.OofSound
local fill = npc.Head.BillboardGui.HealthBar.Health

local Health = npc.Humanoid.Health

fill.Size = UDim2.fromScale(Health/100, 1)

npc.Humanoid.Died:Connect(function()
	sound:Play()
	npc:Destroy()
end)

you can try
this only change the health bar size when npc is spawn.
and play the sound and destroy npc when it dies.

you have to connect to the health changed event to change the size of the health bar whenever it is damaged

I changed it to this, but it wont change the size when its damaged

local npc = script.Parent

local sound = npc.Torso.OofSound
local fill = npc.Head.BillboardGui.HealthBar.Health

local Health = npc.Humanoid.Health

npc.Humanoid.HealthChanged:Connect(function()
fill.Size = UDim2.fromScale(Health/100, 1.01)	
end)

npc.Humanoid.Died:Connect(function()
	sound:Play()
	npc:Destroy()
end)

That’s because you’ve captured the health variable in the beginning. You should instead reevaluate it every time the humanoid is damaged:


local sound = npc.Torso.OofSound
local fill = npc.Head.BillboardGui.HealthBar.Health

--use the health parameter of the healthchanged event
npc.Humanoid.HealthChanged:Connect(function(health: number)
fill.Size = UDim2.fromScale(health/100, 1.01)	
end)

npc.Humanoid.Died:Connect(function()
	sound:Play()
	npc:Destroy()
end)
1 Like

Just one more thing, how can I make it so whoever killed the NPC will get like 50 guts like a leaderstats IntValue? I already created a folder in the player called “leaderstats” with an IntValue inside called “GUTS”

oh well that would require you to write a custom TakeDamage function, and call it everytime you want to deal damage:

local function customTakeDamage(target: Humanoid, sender: Player, dmg: number)
   target:TakeDamage(dmg)
--sender killed target, award guts
   if target.Health <= 0 then
        sender.leaderstats.GUTS.Value += 50
   end
end

and then when you want to deal damage you call this function to handle it instead of reducing the health normally.

Or you could have the guts drop as objects and award guts to whoever picks them up of course.

where would i put that script?

omg, sorry I didn’t get any notification you replied to me.
Anyway, you could return this from a module script, that way you’ll always have access to it. Put the modulescript in serverscriptservice I guess. If you will only damage stuff from one script, then you can just put it in that script.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.