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

display how much health you lost on gui?

tried to make a script where it runs a health:changed event and will display the amount of health you lost on a textlabel from a gui (gui called: HealthChange). the amount of health lost will be the value of a number value in repstorage. if you are confused on what i just said, heres the script.

local status = game.ReplicatedStorage.Status
local humanoid = game.Players.LocalPlayer.Character.Humanoid
local currentHealth = humanoid.Health

humanoid.HealthChanged:Connect(function(health)
	while wait() do
			local change = math.abs(currentHealth - health)
	print("The humanoid's health "..(currentHealth > health and "decreased by ")..change..".")
	status.Value = currentHealth
	game.StarterGui.HealthChange.TextLabel.Text = status.Value
	currentHealth = health
	end

end)

not sure what to do, help

2 comments

0

PlayerGui, not StarterGui

0

oh k

1 answer

Before I jump into what you should do, my suggestion is to use variables instead of userdata values (numbervalues, intvalue, etc.)

Second thing, you do not need that while loop under the changed function, you never break it that's one so it's always going to keep going forever. Two, you only need the GUI to change text every time the player's health changes, which you already met by using the HealthChanged method of Humanoid.

Third thing, just parent this to the screen gui object where the textlabel is in, and use script.Parent, because you were editing the gui under screengui not the one your player has.

Your new script should look like this:

--local status = game.ReplicatedStorage.Status
repeat wait() until game.Players.LocalPlayer.Character -- gotta make sure character has loaded
local character = game.Players.LocalPlayer.Character
local humanoid = character.Humanoid
local currentHealth = humanoid.Health

humanoid.HealthChanged:Connect(function(health)
	if health < currentHealth then -- u need to make sure the health change is decreasing, otherwise it will show you when the player is healing how much it healed by
		local change = math.abs(currentHealth - health) -- thats fine 
		print("decreased by" .. change)
		--print("The humanoid's health "..(currentHealth > health and "decreased by ")..change..".")
		--status.Value = currentHealth -- didnt need to transfer this value to a userdata value, you already have it as a variable so use the variable 
		script.Parent.HealthChange.TextLabel.Text = tostring(change) -- parent this script to the screengui object
		wait(3) -- wait 3 seconds
		script.Parent.HealthChange.TextLabel.Text = "" -- you probably only want to show it for a little bit and then it goes away 
	end
	currentHealth = health -- store the new health -- outside the if statement because we always want to update the health with the current health value
end)

Hope I helped! If you have any questions please let me know.

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