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

What is wrong with this text script?

Asked by
Mystdar 352 Moderation Voter
9 years ago

It is supposed to show your health (Digits) But doesn't It is in the Starter GUI, then a frame, then the script is in a Text in the Frame.

Player = script.Parent.Parent.Parent.Parent.Parent
Character = script.Parent.Parent.Parent.Parent.Parent.Character

while true do
    wait(0.1)
    script.Parent.Text = math.floor(Character.Humanoid.Health)
    end

1 answer

Log in to vote
0
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
9 years ago

Based on your explanation your hierarchy is like this?

StarterGui
    Frame
        TextLabel
            Script

Well your first obvious problem is that the Frame is not inside a ScreenGui instance. All GUI object must be inside a ScreenGui is order to be shown. Second, the Script should be a LocalScript, because it's the descendant of a player.

Other than that, I don't see anything wrong with your script, syntax speaking. There are, however, ways you could make it more readable and more efficient and less glitchy - some of which may be the reason the code is not working.

For example, make your variables local. It's also normally considered the preferred style to make variables lowercase, so that they are more easily distinguished from methods, properties, etc.

Also since you already have a Player variable defined, there is no need to parent all the way up to the player a second time for Character. You need to make sure that the character is loaded, however, before creating the variable, to prevent errors.

Instead of a while loop, it would be better to use the HealthChanged event of Humanoid.

local player = script.Parent.Parent.Parent.Parent.Parent
    repeat wait() until player.Character
local character = player.Character

character.Humanoid.HealthChanged:connect(function()
    script.Parent.Text = character.Humanoid.Health
end)
0
Right, and will this be in the StarterGUI or should I clone it into the PlayerGUI? Mystdar 352 — 9y
0
Depends on what your purpose is. StarterGui is probably sufficient for a health GUI. Perci1 4988 — 9y
Ad

Answer this question