User blog:MaintenanceRequired/How to Script: Making A Health Kit

Please note: I will not be available to make tutorials from December 25–December 27, as I am leaving to go on holiday.

Hi, all!

In this scripting series, I will be doing a set of scripting tutorials. Each tutorial will be based around the community's popular choices, and hopefully will broaden your scripting knowledge. Today's tutorial is: making a basic health kit.

Making the health kit
For this tutorial, we will be going over how to make a health kit. We will start by adding a health kit. For this tutorial, we will just add one part. This part will act as our health kit. Inside the health kit should include a script inside.

Scripting One: Touched Event
In modern first-person shooters, or just shooters, health kits are often fired when they are touched. As a result, we will need to ensure that when a player has touched the kit, it will activate. However, we need to check if the player character has actually touched it. In order to check this, we will need to access the Humanoid.

Open up the Output menu under. It should print "Healing, in progress" in the Output menu. That means the current code we have is working.

Scripting Two: Adding healing values
Now that we know that something will fire when we run over the part, we can start adding values and start healing someone. We need to first insert a value to heal someone by when they do pick it up. The Health section of a Humanoid is where ROBLOX stores someone's current health. The default value / max value is 100, however, you can adjust it to anything you like when you test the game.

Testing
Now, that we have our basics down, we should test out our health kit to ensure everything is right. To do so, head over to your Humanoid, which should be located under  in the Explorer. In the Properties window, find Health and insert any value below the Max Health, and above 0.

When you touch the part, it should appear to add 10 to your health.

Scripting Three: Debouncing
However, when you do test out the code, it seems to instantaneously refill the health. That is because the current code will keep firing when the player touches the part. As a result, we need to counter that by adding a debounce. This ensures that there will be some sort of delay when collecting the health kit.

To do this, we need two values. One is an integer that will act as our "cooldown", and another is a boolean. A boolean always has two values; true and false. For teaching purposes, we will use the following two variables.

We will now use these two variables inside the health kit, to keep track of how often we can use the health kit. Once we heal the character, we will wait 10 seconds before we can heal the character again.

Scripting Four: When can't health kits be picked up?
So far, in this lesson, anyone can pick up the health kit. This is true for first-person shooters, apart from two people; those who have died and those who have maximum health. As a result, we should not allow anyone who has died (Health down to 0), or anyone who's already on full health (Health = Max Health), allowed to pick up the health kits.

Scripting Five: Transparency
Like many first-person shooters, health kits go invisible when they can't be used. This shouldn't be hard to do, and to simply change transparency, we need to adjust its transparency.

Testing
Test your health kit, again. This time, you should see that it will disappear after you successfully use it. Next, you should see that your health increases by 10 after use.

Feedback
Please vote fairly. Any criticism will be appreciated. Thanks again for checking out this scripting tutorial. Requests are now open. Please ask away. Out of five, how useful was the Health Kit tutorial? 1 2 3 4 5