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

display how much health you lost on gui?

Asked by 4 years ago

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

0
PlayerGui, not StarterGui Ziffixture 6913 — 4y
0
oh k ImprinintInc 18 — 4y

1 answer

Log in to vote
1
Answered by
Nanomatics 1160 Moderation Voter
4 years ago
Edited 4 years ago

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.

Ad

Answer this question