PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience

Coding Traps and Pickups

Coding Traps and Pickups

Oct 05 2018, 7:39 PM PST 15 min

In the Making Your Own Obby Game tutorial, you were introduced to Roblox Studio and you designed a basic obstacle course. In this tutorial, you’ll do the following:

  • Think about hazards and pickups in game design
  • Make a dangerous part that destroys any player who touches it
  • Create a pickup that gives the player super jumping ability
  • Learn how to adjust scripts to customize game behavior

Game Design Principle – Hazards and Pickups

Most action games have “hazards” or “traps” that players must avoid. Unlike enemies who move around or chase the player, hazards are usually just part of the game world, but they’re equally dangerous! Some examples are:

  • Lava or fire pits
  • Poison pools
  • Explosive mines

Similarly, almost every game has “pickups” for the player to find and grab. Some examples are:

  • Coins or gems
  • Health packs
  • Booster items that give players special abilities for a short time

Creating a Dangerous Trap

First, let’s create a new part within the obby that will eventually become a dangerous trap.

  1. Select the “MODEL” tab. This shows you the most important buttons and options for working with parts.

  2. Create a new part and position it on top of another platform. Make sure you leave enough safe space (green areas shown below) so that the player can jump over the trap without touching it.

  3. “Anchor” the part in place so that it can’t slide off the platform.

  4. Click the “small arrow” below the “Color” icon.

  5. Select a color from the Roblox “color picker”. Bright red is a good choice because it’s often used as a “danger” color.

Great! Now you have a bright red part which looks different (and hopefully a bit dangerous) so that players will try to jump over it.

Naming the Part

Before you proceed too far, it’s a good idea to enter a custom name for this important part. Doing so helps you find the part in the Explorer window when you need to change something about it.

  • If it’s not still selected in the game editor window, select the trap part. In the “Explorer” window, that part will be highlighted.

  • Notice that the part is just named “Part” — that’s not too helpful, so let’s change it. “Right-click” on the part’s name to show a menu of options. Select “Rename”.

  • Type in “Trap”, a much better name for this part!

Creating a Script

At this point, the new trap part looks different but it doesn’t “do” anything — it’s just another part in your game that players can touch and walk around on.

To make the trap part do something special, it needs a “script”. Scripts are small but powerful bits of “code” that can perform special actions, change things in the game, or do almost anything else you imagine. You’ll use the “Lua” language for Roblox, a very popular language for game scripting.

  • In the Explorer window, *hover* over the part name.
  • Click on the circle (+) button.
  • Scroll through the drop-down list until you find "Script" and click on it.
  • This will create a new script in a new tab and show it.

Modifying the Script

Right now, the script won’t do much — it basically just sits there waiting for you to add some commands. To change that:

  1. “Delete” the entire line print("Hello world!").

  2. Copy and paste in these new lines:

local trapPart = script.Parent
local function onPartTouch(otherPart)
    local partParent = otherPart.Parent
    local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
    if ( humanoid ) then
        -- Set player's health to 0
        humanoid.Health = 0
    end
end
trapPart.Touched:Connect(onPartTouch)

Playtesting

Time for the fun part: testing the new trap!

  • Press the "Play" button.
  • Run and jump around until you reach the trap part. Instead of jumping over it, "touch" it. If you did everything right, your player should instantly explode, sending pieces flying off in all directions.

How it Works

Pretty cool, but how does it work? Let’s explore the script step by step.

  1. The first line just sets a “variable” that tells the script what specific game part it’s linked to. In this case it’s linked to the trap part, the “parent” of the script.
local trapPart = script.Parent
  1. The next part, highlighted here, is a function.
    In computer programming, functions are sets of steps that belong together and you can use them over and over. This function checks if a clumsy player (not some other random part in the world) touched the trap. If so, it sets the player’s health to 0 which instantly destroys him or her. Ouch, that hurts!
local trapPart = script.Parent
local function onPartTouch(otherPart)
    local partParent = otherPart.Parent
    local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
    if ( humanoid ) then
        -- Set player's health to 0
        humanoid.Health = 0
    end
end
trapPart.Touched:Connect(onPartTouch)
  1. The final line, shown here, connects the trap part and the function with a Touched event. That makes the function run whenever the trap part is touched.
trapPart.Touched:Connect(onPartTouch)

3: Making a Jump Booster

Now let’s create a cool “boost pickup” that gives the player the ability to jump super high for a short time.

  • Create a new part and position it somewhere on top of another platform.
  • "Anchor" the part in place so that it doesn’t roll off the platform.
  • Change its color by selecting a new color from the Roblox color picker.
  • "Rename" the part to something like "JumpBoost".

Creating the Script

Just like before, you’ll need a “script” to make this part do something special.

  • In the Explorer window, *hover* over the part name.
  • Click on the circle (+) button.
  • Scroll through the drop-down list until you find "Script" and click on it. Just like before, this will create a new script and show it.
### Modifying the Script

Now, modify the new script.

  1. “Delete” the entire line print("Hello world!").
  2. Copy and paste in these new lines:
local boostPart = script.Parent
-- Set a variable for boosted jump power
local boostedJumpPower = 100
local function onPartTouch(otherPart)
    local partParent = otherPart.Parent
    local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
    if ( humanoid ) then
        boostPart.CanCollide = false
        local currentJumpPower = humanoid.JumpPower
        if ( currentJumpPower < boostedJumpPower ) then
            humanoid.JumpPower = boostedJumpPower
            wait(10)
            humanoid.JumpPower = currentJumpPower
        end
    end
end
boostPart.Touched:Connect(onPartTouch)

Playtesting

Great! Now test the game and see if the booster gives your player superhuman jumping powers.

  • Press the “Play” button.
  • Find your way over to the boost part and run through it. Now try jumping — if you did everything correctly, you should be able to leap higher than you ever could before!

How it Works

Let’s explore these lines of code to see how the script works.

  1. Just like before, the first line sets a variable which tells the script what specific game part it’s linked to, in this case the boost part.
local boostPart = script.Parent
  1. The next part sets a new variable. All variables have associated values and this one is set to the number 100. Later in the script, this variable will be used to increase the lucky player’s jump power when they touch the boost item. Normally, all players in Roblox have a jump power of 50, so this will let them jump much higher.
local boostPart = script.Parent

-- Set a variable for boosted jump power
local boostedJumpPower = 100
  1. After that is a function which has a similar structure to the function you saw before. What it does, though, is quite different. Instead of setting the player’s health to 0, this function:

    • Makes the boost part into a ghost; object “only” for players. This lets players touch the boost but also walk through it without being forced to step over it, a nice little adjustment for pickup items.
    • Sets the player’s current jump power (before boost) to a variable currentJumpPower so that the script has a reference to the normal jump power.
    • Makes sure the player’s current jump power is less than the boosted jump power value. This means the player can only gain a jump boost if they’re not already boosted.
    • If the player isn’t already boosted, it increases his or her jump power.
    • After 10 seconds, it sets the player’s jump power back to the normal jump power which was stored in the variable currentJumpPower.
local boostPart = script.Parent

-- Set a variable for boosted jump power
local boostedJumpPower = 100

local function onPartTouch(otherPart)
    local partParent = otherPart.Parent
    local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
    if ( humanoid ) then
        boostPart.CanCollide = false
        local currentJumpPower = humanoid.JumpPower
        if ( currentJumpPower < boostedJumpPower ) then
            humanoid.JumpPower = boostedJumpPower
            wait(10)
            humanoid.JumpPower = currentJumpPower
        end
    end
end
boostPart.Touched:Connect(onPartTouch)
  1. The final line is almost exactly like the final line in the first script: it simply connects the boost part and the function with a “Touched” event.

Adjusting Script Behavior

As you see, scripts let you add unique game features and special player behaviors. Most scripts can also be changed in cool ways by adjusting or adding a few lines. Let’s look at two examples.

Changing Variable Values

When coding scripts, you will use a lot of variables. Each variable has:

  • A unique descriptive “name” which is used to identify it in other places inside the script. In the super jump script above, boostedJumpPower is a variable name.
  • A “value”, and that value can be changed (variables store information or data which equals one thing when the game starts, but you can change that value as the game runs if necessary).

For the super jump booster script, the “value” of the boostedJumpPower variable was set to “100”. That’s a big increase over the normal jump power of 50, but you may want to make it even higher so that players soar up into the sky as if they have rockets on their feet.

To do that, simply change 100 in the script to something like 250:

-- Set a variable for boosted jump power
local boostedJumpPower = 250

Now run the game and test the effect. This new value should make the player jump high up into the sky!

For most games, the new value of 250 is probably too high. In that case, just reduce it back down to a more reasonable value like “120”:

-- Set a variable for boosted jump power
local boostedJumpPower = 120

Removing the Pickup

When you playtest the game, you’ll quickly notice that the super jump booster doesn’t disappear when the player grabs it. Although the script code does make sure that players can’t keep re-boosting over-and-over during the 10 seconds of power it provides, the part itself stays on the screen.

If you want the pickup to be used only once, you can add one simple command to the script that “destroys” the part when it’s touched (picked up by a player). Inside the function, just add this highlighted line:

local boostPart = script.Parent

-- Set a variable for boosted jump power
local boostedJumpPower = 120

local function onPartTouch(otherPart)
    local partParent = otherPart.Parent
    local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
    if ( humanoid ) then
        boostPart.CanCollide = false
        local currentJumpPower = humanoid.JumpPower

        if ( currentJumpPower < boostedJumpPower ) then
            humanoid.JumpPower = boostedJumpPower
            boostPart:Destroy()
            wait(10)
            humanoid.JumpPower = currentJumpPower
        end
    end
end
boostPart.Touched:Connect(onPartTouch)

Now run the game again and grab the jump booster. If you added the Destroy() command in the correct place, the part should disappear.


Incredible! With this first script, you’re on your way to becoming a game programmer who can use code to make amazing things happen.

Where next? Check out these tutorials which will help you complete your obby.

Tags:
  • intro
  • mechanics
  • coding
  • scripts