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

Coding Traps and Pickups

Coding Traps and Pickups

Sep 03 2019, 12:49 PM PST 15 min

In the articles/Creating Your First Game|Making a Basic Platformer tutorial, you were introduced to Roblox Studio and you designed a basic obstacle course. In this tutorial, you’ll do the following:

  • 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

Creating a Dangerous Trap

Many action games have stationary hazards like spike pits, lava pools, or explosive mines. Let’s create a new part within the obstacle course that will eventually become a dangerous trap.

  1. Create a new part and position it on top of a platform. Leave enough safe space (green areas shown below) so that players can jump over the trap without touching it.
  1. Anchor the part in place so that it can’t slide off the platform.
  1. Click the small arrow below the Color icon.
  1. Select a color from the color picker, for instance “Really red” to indicate it’s a trap.
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.

  1. If it’s not still selected in the game editor window, select the red trap part. In the Explorer window, that part will be highlighted:
  1. Right-click the part’s name and select Rename from the context menu.
  2. 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 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 can imagine.

  1. In the Explorer window, hover over the part name and click the circle button.
  1. 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!

  1. Press the Play button.
  1. Make your way to the trap part, but instead of jumping over it, touch it. If you did everything right, your player should instantly explode.

How it Works

Simple and effective, 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 game scripts, functions are sets of related steps which can be used over and over. This function checks if a careless 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.
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 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)

Making a Jump Booster

Now let’s create a boost pickup that gives the player super jumping ability for a short time.

  1. Create a new part, position it on top of another platform, and change its color using the color picker.
  1. Anchor the part in place so that it doesn’t roll off the platform.
  2. 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.

  1. In the Explorer window, hover over the part name and click the circle button.
  2. 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.
  3. Delete print("Hello world!") and paste in these new lines:
local boostPart = script.Parent
-- Set a variable for boosted jump power
local BOOSTED_JUMP_POWER = 125

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 < BOOSTED_JUMP_POWER then
			humanoid.JumpPower = BOOSTED_JUMP_POWER
			wait(10)
			humanoid.JumpPower = currentJumpPower
		end
	end
end
boostPart.Touched:Connect(onPartTouch)

Playtesting

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

  1. Press the Play button.
  2. Find your way to the boost part and run through it. Now try jumping — if you did everything correctly, you should 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 creates a new variable with a value of 125. Later in the script, this variable will be used to increase the player’s jump power when they touch the boost item.
local boostPart = script.Parent
-- Set a variable for boosted jump power
local BOOSTED_JUMP_POWER = 125
  1. After that is a function which has a similar structure to the function you saw before. Instead of setting the player’s health to 0, however, this function does the following:

    • On line 9, it 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.
    • On line 10, it sets a variable currentJumpPower to the player’s current jump power (before boost) so that the script has a reference to the default jump power.
    • On line 11, it makes sure the player’s current jump power is less than the boosted jump power value. This ensures that the player can only gain a jump boost if they’re not already boosted.
    • On line 12, it increases the player’s jump power to the value of BOOSTED_JUMP_POWER.
    • On lines 13-14, it waits 10 seconds and then sets the player’s jump power back to the default jump power which was stored in the variable currentJumpPower.
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 < BOOSTED_JUMP_POWER then
			humanoid.JumpPower = BOOSTED_JUMP_POWER
			wait(10)
			humanoid.JumpPower = currentJumpPower
		end
	end
end
  1. The final line is almost exactly like the final line in the trap script: it simply connects the boost part and the function with a Touched event.
boostPart.Touched:Connect(onPartTouch)

Adjusting Script Behavior

As you see, scripts let you add unique game features and special object behavior. Most of this can be adjusted by changing or adding a few lines in the script. Let’s look at two examples.

Changing Variable Values

For the jump booster script, BOOSTED_JUMP_POWER was set to 125. That’s a big increase over the default jump power of 50, but you can make it even higher so that players soar up into the sky. To do so, simply change 125 to something like 250:

local boostPart = script.Parent
-- Set a variable for boosted jump power
local BOOSTED_JUMP_POWER = 250

Now play the game and test the effect. This new value should make players jump high up into the sky.

Removing Pickups

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

If you want a 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 function onPartTouch(otherPart)
	local partParent = otherPart.Parent
	local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
	if humanoid then
		boostPart.CanCollide = false
		local currentJumpPower = humanoid.JumpPower
		if currentJumpPower < BOOSTED_JUMP_POWER then
			humanoid.JumpPower = BOOSTED_JUMP_POWER
			boostPart:Destroy()
			wait(10)
			humanoid.JumpPower = currentJumpPower
		end
	end
end

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


With this first script, you’re on your way to making objects in your game do amazing things. Where next? Check out the tutorials below which will help you complete the obstacle course.

Tags:
  • basics
  • studio
  • obby
  • scripts
  • coding