Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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)

1 comment

0

thanks m8

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.