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
Create a new part named SpeedBoost and insert a script.
Name the script and create a comment describing the script’s purpose.
Create a new variable:
local speedBoost = script.Parent
Check for Touched Events
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.
Outside the function, type
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.
local character = otherPart.Parent
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
Create an if/then with the condition
humanoid and humanoid.Walkspeed <= 16
Create a print statement to test if the statement works.
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.
Set the WalkSpeed
Now that the if/then statement works, change the humanoid’s
In the if/then statement:
humanoid.WalkSpeedto a number between
Difference between = and ==
Be sure to use
= to set the value of
== is used whenever comparing two values in an if/then statement.
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
WalkSpeedback to the starting value of
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
- Check that
Walkspeedis 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.