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