Roblox Studio · Lesson 9

Roles and values of life: lava production and restoration stations

Knows Player, Character and Humanoid, using each role as a stand-alone cooler to make blood-tied and returned.

10–15 years old 60 minutes Humanoid / Health Hands-on practice Testing and Saving

Course overview

Stepping on the lava will shed blood and coming to the recovery station will be restored.

This time, the organs will not only change color, but will also alter the life value of the role. Let's set the damage very small and give you time to observe, leave and try again.

Player is the player record, Character is the character model in the scene, and Humanoid manages health and movement. Finding a Humanoid does not yet guarantee that it belongs to a real player, as NPCs can also possess Humanoids.

01 · BEFORE

Before starting

Understand Touched, functions, if and tables. Use the new Lesson09 Baseplate.

02 · MAKE

Your work

A piece of Lava that deducts up to 10 health points per second, and a HealPad that restores up to 10 points per second.

03 · CHECK

Complete flag

Ordinary Part does not trigger; each of the two players has a cooldown; the health value will not exceed MaxHealth.

An official scene showing lifelines when the role is injured.
Figure 9-1: Official health bar screenshot: After the character is injured, the life bar can provide visual feedback; Humanoid.Health should still be checked when judging the value.Roblox Creator Documentation · CC BY 4.0 · The original image has not been modified · Description of the permit

Learning route

Prepare first, then do it; after seeing the result, remember to save it.

Every time a small step is completed, it is tested. Say what you expect to see first, and then use Play and Output to check it. Don't rush to write all the code at once.

1

01·Place two mechanisms on safe ground

Keep the Baseplate first and avoid the trap of failure at first touch. You need to see how your health changes, rather than constantly respawning.

2

02 · Find the player from the encountered parts

The otherPart given to you by Touched may be a leg or an ordinary block. FindFirstAncestorOfClass("Model") first finds the outer model, and then uses GetPlayerFromCharacter to confirm whether it is the player's character.

3

03·Give each character his own cooldown lock

cooldown[character] Record "cooling down" under the name of the corresponding character. If one player touches it, it will not lock another player.

4

04 · Cooldown is not a continuous damage timer

task.delay(1, function() ... end) arranges to clear the record after one second, so that subsequent scripts will not stop in place. The cooldown table records will be cleared and there is no need to permanently save the old character.

5

05 · Check damage, treatment and rebirth separately

Roblox's default character may regenerate health slowly and automatically, making it easier to tell the effect of your own treatment by observing it in a short period of time. Don't mistake automatic recovery after a few seconds for repeated triggering.

Step by step

01·Place two mechanisms on safe ground

Keep the Baseplate first and avoid the trap of failure at first touch. You need to see how your health changes, rather than constantly respawning.

The two mechanisms handle damage and treatment respectively. They use the same script and only differentiate the effects by the number DAMAGE.

  1. Create Lava and HealPad, the Sizes are 8, 1, 8, and the Positions are -8, 0.5, -12 and 8, 0.5, -12 respectively.
  2. Both Anchored, CanTouch, and CanCollide are true; distinguished by red and green colors.
  3. Create a common Script in Lava and name it HealthPad.

Step by step

02 · Find the player from the encountered parts

The otherPart given to you by Touched may be a leg or an ordinary block. FindFirstAncestorOfClass("Model") first finds the outer model, and then uses GetPlayerFromCharacter to confirm whether it is the player's character.

This is more stringent than checking Humanoid alone. If you can't find a model, a player, or a surviving Humanoid, you return it ahead of time without changing any life value.

  1. Read three checks in the next script.
  2. Remember, Health is the current life, Max Health is the upper limit of life.
  3. When you see nil, first check whether the object has been found. Do not add more waits immediately.
Humanoid display properties panel screenshot
Figure 9-2: Official Humanoid attribute panel: First select the Humanoid in the character. The picture shows the display settings. What we are looking for in this lesson is Health and MaxHealth.Roblox Creator Documentation · CC BY 4.0 · The original image has not been modified · Description of the permit

Step by step

03·Give each character his own cooldown lock

cooldown[character] Record "cooling down" under the name of the corresponding character. If one player touches it, it will not lock another player.

Using TakeDamage to deduct blood can respect ForceField protection. It is normal that no blood will be lost when the protective shield is present after birth. Wait for the protection to end before testing; for treatment, use math.min to limit the upper limit.

  1. Place the following code into Lava > HealthPad.
  2. Copy HealthPad to HealPad and only change DAMAGE to -10.
  3. After playing, wait for the birth protection to end, step on the red block and then the green block; check the health value of Output.
Workspace > Lava / HealPad > HealthPad · One copy of eachLuau
local Players = game:GetService("Players")
local pad = script.Parent
local DAMAGE = 10 -- HealPad 内改为 -10,表示恢复
local cooldown = {}

pad.Touched:Connect(function(otherPart)
    local character = otherPart:FindFirstAncestorOfClass("Model")
    if not character then return end
    local player = Players:GetPlayerFromCharacter(character)
    local humanoid = character:FindFirstChildOfClass("Humanoid")
    if not player or not humanoid or humanoid.Health <= 0 then return end
    if cooldown[character] then return end
    cooldown[character] = true

    if DAMAGE > 0 then
        humanoid:TakeDamage(DAMAGE)
    else
        humanoid.Health = math.min(humanoid.MaxHealth, humanoid.Health - DAMAGE)
    end
    print(player.Name, "Health:", humanoid.Health)
    task.delay(1, function()
        cooldown[character] = nil
    end)
end)

Step by step

04 · Cooldown is not a continuous damage timer

task.delay(1, function() ... end) arranges to clear the record after one second, so that subsequent scripts will not stop in place. The cooldown table records will be cleared and there is no need to permanently save the old character.

Touched is a contact event, which is not guaranteed to be triggered every second when standing still. The rule here is "When a new contact comes, it can be processed at most once per second", not "As long as you stand on it, your blood will be deducted every second."

  1. Repeatedly walk down and up the red block and observe the output interval.
  2. Try it once while standing still, and record the difference between walking back and forth.
  3. Put the ordinary Part on the mechanism and confirm that there is no player health message.

Step by step

05 · Check damage, treatment and rebirth separately

Roblox's default character may regenerate health slowly and automatically, making it easier to tell the effect of your own treatment by observing it in a short period of time. Don't mistake automatic recovery after a few seconds for repeated triggering.

If the green block also deducts blood, what is checked is the DAMAGE inside the green block, not the red block script. When debugging, confirm which object you have selected.

  1. After being injured, step on the HealPad and observe that the life increases but does not exceed the upper limit.
  2. When tested with two players, one person's cooldown did not stop the other.
  3. Retesting after rebirth to confirm that the new role is still operational.

Homework

It's your turn to change and make your own version.

1 Gentle Trap

Change the damage to 5 and the treatment to 15, and test them separately.

2 Draw object relationships

Draw the relationship between Player, Character, and Humanoid, and mark the location of Health.

3 Select: Touch feedback

Add a print containing the organization name to distinguish Lava and HealPad messages.

Completion criteria:First verify the work in Playtest, then click Stop and save it as "Initials_Lesson09" or the corresponding Homework copy; do not overwrite the runnable version of the previous lesson.

Exit questions

Answer it yourself first, and then expand on it.

Does the model with Humanoid have to be a player?

No, NPCs may also have them. GetPlayerFromCharacter is used to confirm whether it belongs to the player.

Why use cooldown[character]?

Let the different players cool down and avoid sharing a lock among all players.

The red block has no blood, why wait for a while?

The post-birth ForceField may be protecting the character, and TakeDamage will respect this protection.

Does Touched plus cooldown equal continuous area damage?

No, it only handles contact events, and it is not guaranteed to occur once per second when standing still.

Explore further

This is where we start when we need to check the data.

The text code can be copied directly. First confirm the script type, placement, and whether this code is a complete version or a small experiment; do not run multiple versions of the same script superimposed. The source of the interface reference picture has been marked. The object names in the picture may be different from those in this lesson. Please refer to the operation steps.

Download the code and placement instructions for this lesson