Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why does my health bar script break, and not work well?

Asked by 5 years ago

I'm not sure what is going on what so ever. (I'm new to coding so excuse the messiness) Ok so, I have a health bar that decreases according to a dummy's health. For some reason, it is always laggy and never consistent. It also breaks every now and then. Here is my code for the dummy health bar.

wait (.1)
local visible = script.Parent.Parent.Parent.Visible
local bframe = script.Parent.Parent.Parent.Parent
while true do
    local hp = game.Workspace.Dummy.Humanoid.Health/500
    script.Parent:TweenSize(UDim2.new(hp,0,1,0),Enum.EasingDirection.Out,Enum.EasingStyle.Quad,0.15)
    wait (0.1)
    if hp < 0.001 or hp == 0 then
        bframe.BaseFrame.Visible = false
        wait (7)
        bframe.BaseFrame.Visible = true
        script.Parent:TweenSize(UDim2.new(0,647,0,70))
        script.Parent:TweenSize(UDim2.new(hp,0,1,0),Enum.EasingDirection.Out,Enum.EasingStyle.Quad,0.15)
        local hp = game.Workspace.Dummy.Humanoid.Health/300
        script.Parent:TweenSize(UDim2.new(hp,0,1,0),Enum.EasingDirection.Out,Enum.EasingStyle.Quad,0.15)
    end
    wait (.2)
end

if you need more code anywhere let me know. Also here is a link to my game so you can just see for yourself what happens.

https://www.roblox.com/games/659823610/Game-Tester#!/about

thank you, and sorry about how messy the code is. I just need to figure out what is happening for some reason.

1 answer

Log in to vote
1
Answered by
mado 40
5 years ago
Edited 5 years ago

You should use the .HealthChanged event of the Humanoid instead of having a while loop, it's much more efficient. Here's an example of how you can use it with your game:

local Dummy = workspace:WaitForChild("Dummy")

Dummy.HealthChanged:Connect(function(newHealth)
    script.Parent:TweenSize(UDim2.new(0,647,0,70))
    script.Parent:TweenSize(UDim2.new(newHealth/500,0,1,0),Enum.EasingDirection.Out,Enum.EasingStyle.Quad,0.15)
    script.Parent:TweenSize(UDim2.new(newHealth/300,0,1,0),Enum.EasingDirection.Out,Enum.EasingStyle.Quad,0.15)
end)
0
Holy moly wow thank you so much. That is way smaller than my code. Just_Moop 27 — 5y
Ad

Answer this question