If statements can have multiple requirements that need to be true before running code. The keyword and allows you to combine statements. The following code evaluates first if two plus two equals six and then if four does not equal six. If both statements are true, the code will run.
-- will not runif 2 + 2 == 6 and 4 ~= 6 thenprint("Both statements are true")end-- will runif 4 + 2 == 6 and 4 ~= 6 thenprint("Both statements are true")end
Creating a Powerup
Powerups are in-experience items that give players special abilities like flying, invisibility, or speed. This powerup will boost the player's walking speed every time the powerup is touched. Continuously applying boosts can make the player go way too fast, so and will be used to control the upper walking speed limit.
Setting Up the Powerup
Use this code with a simple part or a model, such as a crystal, coin, or glowing neon orb.
Create a new part named Powerup and insert a script named WalkSpeedManager.
Declare a variable named speedBoost and assign the script's parent object.
-- Gives a temporary speed boost when touchedlocal speedBoost = script.ParentSet up a function named onTouch and connect it to the parent object's Touched event. Then playtest and check your work.
local speedBoost = script.Parentlocal function onTouch(otherPart)print("Something touched speedBoost")endspeedBoost.Touched:Connect(onTouch)The WalkSpeed property is found on Humanoid objects. Use the same pattern used when creating a trap part and create a conditional that checks for Humanoid objects.
Looks for Humanoid partslocal function onTouch(otherPart)local character = otherPart.Parentlocal humanoid = character:FindFirstChildWhichIsA("Humanoid")if humanoid thenprint("A Humanoid was found")endend
Speeding Players Up
The speed boost will make avatars walk faster every time the speed boost is touched. That will quickly become very, very fast. The keyword and will ensure players can't go too fast by only enabling the speed boost if the player is under a certain speed.
If a Humanoid is found, take the current WalkSpeed value and add 10 to it. Playtest, and your avatar will get faster every time it touches the speed boost.
Increases current WalkSpeedlocal function onTouch(otherPart)local character = otherPart.Parentlocal humanoid = character:FindFirstChildWhichIsA("Humanoid")if humanoid thenhumanoid.WalkSpeed += 10endendspeedBoost.Touched:Connect(onTouch)In the if statement, use the keyword and to add a second condition that the current WalkSpeed value is less than 50. After adding the boost, the fastest WalkSpeed value will be 60.
Checks if current WalkSpeed is 50 or lessif humanoid and humanoid.WalkSpeed <= 50 thenhumanoid.WalkSpeed += 10end
Fine Tuning the Speed Boost
OnTouch is called every time the speed boost is touched. Every step or slightest bounce triggers the Touched event and calls the connected function. The part's property, CanTouch can keep the Touched event from firing. Take advantage of CanTouch and turn off the speed boost for one second every time it's been activated.
After applying the boost, set the part's CanTouch property to false. Playtest and make sure the boost only applies once.
Disables the speed boostlocal speedBoost = script.Parentlocal function onTouch(otherPart)local character = otherPart.Parentlocal humanoid = character:FindFirstChildWhichIsA("Humanoid")if humanoid and humanoid.WalkSpeed <= 50 thenhumanoid.WalkSpeed += 10speedBoost.CanTouch = falseendendspeedBoost.Touched:Connect(onTouch)Use task.wait(1) to pause the script for one second, then set CanTouch to true. Playtest and make sure the speed boost can be reapplied after one second.
Finished scriptlocal speedBoost = script.Parentlocal function onTouch(otherPart)local character = otherPart.Parentlocal humanoid = character:FindFirstChildWhichIsA("Humanoid")if humanoid and humanoid.WalkSpeed <= 50 thenhumanoid.WalkSpeed += 10speedBoost.CanTouch = falsetask.wait(1)speedBoost.CanTouch = trueendendspeedBoost.Touched:Connect(onTouch)Play with the values in the finished script. WalkSpeed can go up to 100. The default WalkSpeed value is 16.
Summary
The keyword and can be used to require multiple conditions before running a code chunk, such as a value being more than 0 and less than 100. Or that if there is a Humanoid and its WalkSpeed is less or equal to 50.