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

How do You Script a basic Health Gui? [Need a health gui for game]

Asked by
alsome495 -21
7 years ago

How would i script a Heath Gui? I have a Call of duty type game but i need a heath gui and i don't know to script it.

1 answer

Log in to vote
3
Answered by 7 years ago
Edited 7 years ago

Well, to accomplish this, you'll require (to/ the):

  1. Retrieve the player's Humanoid

  2. Use a Variable(s) to define the player's humanoid

  3. Use a Function(s) so that we don't rewrite code over-and-over/ to make it simpler & easier

  4. Use the almighty HealthChanged event! >>:O (It fires whenever the player's humanoid's health changes, essentially ;P ) This'll be used to help keep track of the player's health

  5. Use math! (I like math, personally, so not much of a problem here ;) )

However, to note, I'm not going to create an entire script for you; I'll only give you an example(s) which can be used as a reference(s).

Alright, lets begin!

First off, as said previously, we need to retrieve the player's Humanoid & use a Variable to define it, so, lets do just that! BUT, we'll require the WaitForChild as a just-in-case. ;) (More on this later)

--         \/ Variable
local playersHumanoid = WHEREVERCHARACTERIS.Character:WaitForChild('Humanoid')
-- /\ Defined object/ child returned

Ok, now we have our variable & humanoid retrieval-thing! :D This'll be very helpful later on! :O

Second, let us now create what is known as a Function!!! >>:O We'll use this for the code, so that we don't have to rewrite over-and-over/ it'll make it easier. :)

local playersHumanoid = WHEREVERCHARACTERIS.Character:WaitForChild('Humanoid')

function onHealthChange() -- onHealthChange is the name of the function ;P

end

There we go! Almost there! :D Now when we go onto setting up the HealthChanged event, this'll make it a LOT easier. :)

Third, lets add math to the function:

local playersHumanoid = WHEREVERCHARACTERIS.Character:WaitForChild('Humanoid')

function onHealthChange()
    local playersHealth = playersHumanoid.Health -- I couldn't help but define these w/ variables either >.<
    local playersMaxHealth = playersHumanoid.MaxHealth -- Otherwise it would've looked ugly for the following line ;-;
    print(playersHealth, playersMaxHealth, playersHealth / playersMaxHealth  * 100) -- The first variable will print the player's current health (when the player's health has change), the player's max health, and calculate the current health divided by the max health then multiplying it by 100 to get a number (that isn't a decimal; I mean, you could, but I'm not doing GUIs in this example lol)
end

Phew, that was a lot! >-> Now we have our variables (I couldn't help it ;-;), and our math calculation which is shown by the Print function! (More on this later)

Fourth, lets add the HealthChanged event into the equation! >>:D (Not into the print/ math, I mean into the code... Heheheh..?) It'll keep track & fire when the player's health changes:

local playersHumanoid = WHEREVERCHARACTERIS.Character:WaitForChild('Humanoid')

function onHealthChange()
    local playersHealth = playersHumanoid.Health
    local playersMaxHealth = playersHumanoid.MaxHealth
    print(playersHealth, playersMaxHealth, playersHealth / playersMaxHealth  * 100)
end

playersHumanoid.HealthChanged:connect() -- Will fire whenever the player's health changes; the HealthChanged event can only be used on the player's Humanoid, and no other object (b/c, well, the name of the event, DURR lol )

And boom, we're done! :D

Just kidding. ;P

Finally, now (lol) we're going to put the Function's name into the :connect() slot (or event... keeper... fire-er......... Idk ;-;) to, well, connect the function to the HealthChanged event when it fires, and to execute the code on-fire/execution.

local playersHumanoid = WHEREVERCHARACTERIS.Character:WaitForChild('Humanoid')

function onHealthChange()
    local playersHealth = playersHumanoid.Health
    local playersMaxHealth = playersHumanoid.MaxHealth
    print(playersHealth, playersMaxHealth, playersHealth / playersMaxHealth  * 100)
end

playersHumanoid.HealthChanged:connect(onHealthChange) -- Now whenever the HealthChanged event fires, it'll call on good ol' onHealthChange!! >>:D

And wa-la! We're finished! :D Again, this is only a reference-type of script, not an entire script; that's your job man. lol

Stuff touched on, but didn't go into great detail about

  1. The WaitForChild Function - Will yield (or, in simpler terms, pause) the code until a child w/ the exact name as the argument given to the WaitForChild function spawns/ exists w/in the specific parent/ object. (A bit of a mouthful T-T) If you don't quite understand, you may want to check out the Wiki. :P (To get to the page, click on the WaitForChild name as the start of this paragraph) To note, before going onto the next paragraph, the WaitForChild function will NEVER return nil, b/c it waits for the child/ object.

  2. The Print Function - Will print/ enter information ( being strings, numbers, tables, etc) given/ returned into the Output. :P

  3. The Output - Every scripters best friend in the coding world! The Output will show the information (whether being the examples I listed in the Print expanation, or errors & warnings) given to it.

If you wish to check out the pages on the Wiki, click on the highlighted word(s) above! :D

Hope this helped you in any way! :D

0
Tyvm for accepting! :D TheeDeathCaster 2368 — 7y
Ad

Answer this question