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

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

Asked by 5 years ago
Edited 5 years ago

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

0
whats the error? fanofpixels 718 — 5y
0
use localplayer instead of 'script.Parent.Parent.Parent', it's just ridiculous to read Vulkarin 581 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago

"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.

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 CommanderCaubunsia 126 — 5y
0
My codes are fine, and if it worked pls accept User#24403 69 — 5y
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 IDKBlox 349 — 5y
Ad
Log in to vote
-2
Answered by 5 years ago
Edited 5 years ago

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.

0
really? you test it out and prove to me this somehow generates an error SerpentineKing 3885 — 5y
0
*This is why you dont spoonfeed* RubenKan 3615 — 5y
0
I never said it would cause an error. You're committing the straw man fallacy. User#24403 69 — 5y
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  SerpentineKing 3885 — 5y
View all comments (4 more)
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) User#24403 69 — 5y
0
2. 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 SerpentineKing 3885 — 5y
0
sid youtube User#24403 69 — 5y
0
nope, that's not it, guess again, hint: remove the dash, or are you unable to determine a little cipher? SerpentineKing 3885 — 5y

Answer this question