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

How can i make a working health bar(number)?

Hello, this is easy, i made a script but i see nothing wrong with it, but i dont see the textlabel changing text :(

StarterGui(folder) Health(ScreenGui) health(TextLabel) healthcheck(normal Script)

the healthcheck script:

while true do
	script.Parent.Text = script.Parent.Parent.Parent.Parent.Character.Humanoid.Health
	wait(1)
end

why this no work? parent of that is health, perent of that is Health, parent of that is StarterGui, last .Parent is Parent of StarterGui, which when playing, is the Player, then Character is its character then humnaoid then health, i see no errors, but why doesnt it set the variable when i damage myself by touching a testbrick i made that takes away 50 health? the health take away thing works, for the default health bar, but the number for textlabel doesnt show health! By the way right now im more focused on number and i dont care about rounding it to a whole number... please help

2 comments

0

whats the error?

0

use localplayer instead of 'script.Parent.Parent.Parent', it's just ridiculous to read

2 answers

✓ Accepted answer

"Normal" (server) scripts do not run in PlayerGui. This was possible with experimental mode, however experimental mode has been removed and this is now no longer possible.

The fix is easy:

Change the script to a local script.

The server has absolutely no reason to be inside a GUI. In addition, you should use the humanoid.HealthChanged event instead of a while loop. The codes only need to run when the health changes, not every second.

Also use LocalPlayer rather than crap tons of script.Parent

local client = game:GetService("Players").LocalPlayer
local humanoid = (client.Character or client.CharacterAdded:Wait()):WaitForChild("Humanoid")

humanoid.HealthChanged:Connect(function(health)
    script.Parent.Text = tostring(health)
end

health is the new health of the humanoid.

3 comments

0

i DID use health changed, im not stupid! I just changed it because the script as i said wasnt working, but it wasnt the problem obviously, oh and by he way this works, but the problem is that when health CHANGED then it does this, so what about when you join the game? eh whatever, i can just set a datastore with health(in te game you can gain more healthmax) and bring information when character jo

0

My codes are fine, and if it worked pls accept

0

This will work, of course if you remember the ')' at the end of line 6 and if the gui resets upon death, otherwise you'd need to do it different

Local Script within StarterGui

local player = game.Players.LocalPlayer

while true do
	if player.Character ~= nil and player.Character:FindFirstChild("Humanoid") ~= nil then
		script.Parent.Text = player.Character.Humanoid.Health
	end
	wait(1)
end

The main issue with the script is that there is no wait time and since the player character doesn't load at the same time as the server, then there is no character or humanoid to check the health of.

Within the loop, you can check if there is a character and humanoid before causing the change in the textlabel as shown above.

8 comments

0

really? you test it out and prove to me this somehow generates an error

0

This is why you dont spoonfeed

0

I never said it would cause an error. You're committing the straw man fallacy.

0

Now rly, it's laughable that you'd downvote to simply have your answer above mine. Besides, he asked for the problem, not for you to alter the function. In addition: "wrong wrong wrong" implies this doesn't work wouldn't it? @incapaxx, that is what you said after all 

0
  1. I didn't -1 you 2. you're a hypocrite since according to circumstantial evidence YOU -1 others to get yours to the top 3. wrongcould mean you spread a piece of misinformation ect (you say the character didnt load in when this isnt the issue)
0
  1. Alright, well then tell we what the misinformation is? 3. Circumstantial evidence has no legal backing, and you yourself have no proof. 1. Maybe you should take that "straw man" and SID-YT
0

sid youtube

0

nope, that's not it, guess again, hint: remove the dash, or are you unable to determine a little cipher?

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