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

UDim2 size isn't working. How do I make my HealthBar GUI reset?

Asked by
VVired 28
7 years ago

Script doesn't work, I've been trying to make a healthbar reset script, so when they die it resets the gui.

if game.Players.LocalPlayer.Character.Humanoid.Health == 100 then
    script.Parent.Size = UDim2.new(1, 0, 0, 20)
end

2 answers

Log in to vote
0
Answered by 7 years ago

When the player resets, when they spawn back, the script won't run again, so when the player joins the game, it will run the script above and not run again.

Instead, you can use a loop:

while true do
    if game.Players.LocalPlayer.Character.Humanoid.Health == 100 then
        script.Parent.Size = UDim2.new(1, 0, 0, 20)
    end
    wait()
end

or make the script run when the player respawns:

game.Players.PlayerAdded:connect(function(player)
    player.CharacterAdded:connect(function(character)
        if game.Players.LocalPlayer.Character.Humanoid.Health == 100 then
            script.Parent.Size = UDim2.new(1, 0, 0, 20)
        end)
    end)
end)
0
thanks m8 VVired 28 — 7y
Ad
Log in to vote
0
Answered by
Pyrondon 2089 Game Jam Winner Moderation Voter Community Moderator
7 years ago

The problem with your current code is that it will only run once, when the script initially runs. You want the resize to happen upon a certain thing, or certain event, occurring. That's what roblox has events for.

For this case, you're probably looking for either the Humanoid.Died event or the Player.CharacterAdded event. The latter would likely work best, since you presumably want the GUI to reset when the character respawns:

game.Players.LocalPlayer.CharacterAdded:Connect(function()
    script.Parent.Size = UDim2.new(1, 0, 0, 20);
end)

Hope this helped.

P.S.: Not sure if this will be an issue for the way you have yours set up, but just in case, there's a member of ScreenGui called ResetOnSpawn. You're probably going to want that to be false. Cheers.

Answer this question