Coding Traps and Pickups

Submitted by FireBlaze on Sat, 06/15/2019 - 20:03

Open the Baseplate

Templates are pre-built Roblox projects that you can use as a start for your own games. There’s an existing obby template you can test to get an idea of what you’ll be making.

  1. In Roblox Studio, click the New button in the upper-left. If you have any projects open, first close them (FileClose) to access the New button.
  2. Find the template named Baseplate and click the tile to open it.

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 Part and position it above the Baseplate. This part will be our hazardous object. You may like to change the colour to red, to let the player know not to touch it. 

  3. Having a red block that does nothing is not going to do helpful in our game. In order to be able to code it to do something, we need to add a script. Add this script below the Part.

  4. Delete the default scripts (print("Hello Word")) and place this in:

    local Debouce = false

    local part = script.Parent

    part.Touched:Connect(function(hit)

    if hit.Parent:FindFirstChild("Humanoid") and Debouce == false then

    Debouce = true

    hit.Parent.Humanoid:TakeDamage(20)

    wait(0.2)

    Debouce = false

    end

    end)

  5. This slab of code checks if a Player is touching the Part and if so, the player will take damage. Every single time the player hits the part, the player will take 20 damage. However there is a cooldown of 0.2 seconds to ensure the possibility of dieing in less than a second impossible.

  6. Play the game and touch the part. If you do not take damage, please check and double check if you have the right piece of code.

 

Creating a Pickup

After making the hazardous object, let's create a new part which would become our Jump Boost later.

  1. Like the hazardous object, create a new part under the workspace. This will eventually become our Jump Boost Object.
  2. We can change the colour of this Part to a green colour, to single out the difference between hazardous objects and good ones. 
  3. Now that we have a green block, we can add a script under it. Delete the default scripts.
  4. Add this slab of code in:

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

local boostedJumpPower = 100

  1. This 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

local boostedJumpPower = 100

local function onPartTouch(otherPart)

local partParent = otherPart.Parent

local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")

if ( humanoid ) then

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.

Removing the Pickup

When you play-test 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 bolded line:

local boostPart = script.Parent

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

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.

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

 

Well Done!

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