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

how would I print a humanoid's health into a text box?

Asked by 2 years ago
Edited 2 years ago

I'm currently trying to make a game with a humanoid's health needing to be displayed. I'm unsure if it's the code that's the issue, or the placement of the script in the workspace.


workspace = game:GetService(("Workspace")) local text = game.StarterGui.SnailHealth.hp local snailhp = game.Workspace.snail.Humanoid.Health local snailmaxhp = game.Workspace.snail.Humanoid.MaxHealth text.Text = "juan"

the juan was to test, for clarification, and it didn't work ****UPDATE: SOMETHING WITH THE SNAIL IS NOT WORKING IT'S HEALTH RETURNS NIL UPDATE: THIS IS FIXED

0
do you get any errors? in line 1, it seems like you added too many brackets. PaleNoobs 37 — 2y
0
i got it figure out CheemsTheReaper 2 — 2y

2 answers

Log in to vote
0
Answered by 2 years ago

It seems like you have too many brackets at line 1. I think the reason the text doesn't change is because the extra brackets may confuse the script and pause the whole script because of your error. Here's what you can do, just copy paste this code and let me know if it works.

workspace = game:GetService("Workspace")
local text = game.StarterGui.SnailHealth.hp
local snailhp = game.Workspace.snail.Humanoid.Health
local snailmaxhp = game.Workspace.snail.Humanoid.MaxHealth

text.Text = "juan"

If it works, please mark this as an answer. If it doesn't, just let me know.

Ad
Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

The GUI's inside StarterGui are replicated intoa player's PlayerGui when they join. The reason as to why it didn't work is because you were trying to change the Text inside the UI in StarterGui, not the one that the client can see. To change this, you may change your script to:

--This would be a local script parented to the Gui.
-- You do need to define 'workspace'. 
local text = game.Players.LocalPlayer.PlayerGui.SnailHealth.hp -- It's now looking for the GUI inside the local PlayerGui folder instead of the StarterGui.
local snailhp = workspace.snail.Humanoid.Health
local snailmaxhp = workspace.snail.Humanoid.MaxHealth

text.Text = tostring(snailhp).."/"..tostring(snailmaxhp)

This is a screenshot showing what my Explorer looks like.

This is a screenshot showing what the Text says when I hit Play.

To update the text to always say its health, you can add a function.

workspace.snail.Humanoid:GetPropertyChangedSignal("Health"):Connect(function()
    snailhp = workspace.snail.Humanoid.Health
    text.Text = tostring(snailhp).."/"..tostring(snailmaxhp)
end)

This function uses GetPropertyChangedSignal, to make it run every time the Health changes.

By the end, the script should look something like:

local text = game.Players.LocalPlayer.PlayerGui.SnailHealth.hp
local snailhp = workspace.snail.Humanoid.Health
local snailmaxhp = workspace.snail.Humanoid.MaxHealth

text.Text = tostring(snailhp).."/"..tostring(snailmaxhp)

workspace.snail.Humanoid:GetPropertyChangedSignal("Health"):Connect(function()
    snailhp = workspace.snail.Humanoid.Health
    text.Text = tostring(snailhp).."/"..tostring(snailmaxhp)
end)

Answer this question