Powerups with if/then Statements

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.


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


Check for Touched Events

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


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)


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


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")


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


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


  3. Click Run and check your code.


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.

The current code should be like below.

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.


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.


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


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.

Previous Traps with if/then Statements Next Returning Values from Functions