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

Powerups with if/then Statements

15 min

Powerups with if/then Statements

Power-ups give players special abilities like flying or invisibility, or in this case speed! To create a speed boost powerup that temporarily makes players walk extra fast, the if/then statement will check if the object touching the speed boost is a player and if they haven’t already been powered up at the same time.

speedPowerup_finalExample.gif

Setting up the Script

Create a Script and Part

  1. Create a new part named SpeedBoost and insert a script.

  2. Name the script and create a comment describing the script’s purpose.

  3. Create a new variable: local speedBoost = script.Parent

    powersupsUpdated_1.png

Check for Touched Events

  1. Create a function named onTouch and include a print statement inside.

    epowerupsUpdated_2.png

Debugging with the Print Function

Adding print statements in functions is a good way of checking if code is working. If one print statement is missing, you know the code stopped working before that print statement and can better find a potential error.


  1. Outside the function, type speedBoost.Touched:Connect(onTouch)

    powerupsUpdated_3.png

  2. Click Run to check if your print message shows up in the Output window when SpeedBoost is touched.

    speedBoost_test_optimized.gif

Other Things Touching the Part

When you run the code, you may notice the print line appears. This is because the part might already be touching something, like another part or terrain. Whenever your player walks on the speed boost part, a new print line should appear.


Store the Humanoid

Now that the part knows something is touching it, the script needs to check if it’s a humanoid using the FindFirstChildWhichIsA() function. If a humanoid is touching the part, it’ll be stored in the humanoid variable.

  1. Type local character = otherPart.Parent

  2. Type local humanoid = character:FindFirstChildWichIsA("Humanoid")

    powersupUpdated_4.png

You code should now be:

Creating a Speed Boost

Before activating the speed powerup, your code should check if both of these conditions are true:

  • The object touching the speedBoost has a Humanoid.
  • The Humanoid object’s WalkSpeed is 16.

WalkSpeed controls how fast a player’s character moves in the game. The normal value is 16. To check for both conditions at once, use keyword and.

  1. Create an if/then with the condition humanoid and humanoid.Walkspeed <= 16

    powersupsUpdated_5.png

  2. Create a print statement to test if the statement works.

    powerupsUpdated_6.png

  3. Click Run and check your code.

    speedBlockTest.gif

Humanoid Properties - WalkSpeed

If you’re not getting the output you expect, check that you have two ends, one for your if/then statement, and a second end for your function.


Code Example

Set the WalkSpeed

Now that the if/then statement works, change the humanoid’s WalkSpeed property.

In the if/then statement:

  1. Change humanoid.WalkSpeed to a number between 16 and 100.

    powerupsUpdated_7.png

Difference between = and ==

Be sure to use = to set the value of WalkSpeed. == is used whenever comparing two values in an if/then statement.


  1. On the next line, type wait(2) to set how many seconds the speed boost will last. While this lesson uses 2, you can use any number between the ().

  2. Change WalkSpeed back to the starting value of 16.

    powerupsUpdated_8.png

  3. Run your code. The player should get a speed boost and go back to their normal walking speed after 2 seconds.

    speedPowerup_finalExample.gif

Finished Code Example

Final Project Example

You can download an example of this project here.


Troubleshooting Code

  • Check that Walkspeed is between 10 and 100. If not, you may get unexpected errors.
  • Make sure there is a wait() function in between switching the player’s speed. If not, the two changes will happen instantaneously and it’ll look like nothing happened.

These documents are licensed by Roblox Corporation under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Roblox, Powering Imagination, and Robux are trademarks of Roblox Corporation, registered in the United States and other countries.