Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering Roblox Coding

You're reading from   Mastering Roblox Coding The unofficial guide to leveling up your Roblox scripting skills and building games using Luau programming

Arrow left icon
Product type Paperback
Published in Aug 2022
Publisher Packt
ISBN-13 9781801814041
Length 424 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Mark Kiepe Mark Kiepe
Author Profile Icon Mark Kiepe
Mark Kiepe
Arrow right icon
View More author details
Toc

Table of Contents (16) Chapters Close

Preface 1. Part 1: Start Programming with Roblox
2. Chapter 1: Getting Up to Speed with Roblox and Luau Basics FREE CHAPTER 3. Chapter 2: Writing Better Code 4. Chapter 3: Event-Based Programming 5. Part 2: Programming Advanced Systems
6. Chapter 4: Securing Your Game 7. Chapter 5: Optimizing Your Game 8. Chapter 6: Creating User Interfaces for All Devices 9. Chapter 7: Listening to User Input 10. Chapter 8: Building Data Stores 11. Chapter 9: Monetizing Your Game 12. Part 3: Creating Your Own Simulator Game
13. Chapter 10: Creating Your Own Simulator Game 14. Index 15. Other Books You May Enjoy

Exercise 1.2 – Police system part I

This exercise creates a simple police system that calculates a ticket price based on input.

System description

The police want a system where they can set a variable for the speed that a driver was going at, and a variable where they can set whether the driver had a license with them. There should be variables that determine the height of the ticket for each crime. There should also be two variables that state the maximum speed the driver is allowed to go and whether it is required for them to have a license. Combining this data should give one ticket price even if multiple crimes were committed. If there were no crimes committed, the ticket price would be 0. The ticket price should be displayed in Output with the following text: Ticket Price: 0. The number depends on the height of the ticket.

Try to conclude what variables you need based on the system description. Again, analyzing a problem helps you to create a correct system.

Based on the system description, we can conclude the following facts:

  • There should be two variables that the police can set. These variables are for the speed (speed) and whether the driver had a license (hasLicense) with them.
  • There should be two variables (constants?) that determine the ticket price for each crime.
  • There should be two variables (constants?) that determine the maximum allowed speed and whether it is required to have a driver’s license.
  • There should be a variable that holds the height of the ticket (ticketPrice).

Now that we know this, let us start programming our system. Follow these steps:

  1. Open a new baseplate in Roblox Studio.
  2. Create a new script in ServerScriptService.
  3. Create the variables we concluded from the system description.
  4. Create an if statement that checks whether the driver was going over the speed limit and applies the following:
    • If the driver was going over and not at the speed limit, increase the ticket price
    • If the driver was not going over the speed limit, do nothing
  5. Create an if statement that checks whether the driver was violating the driving license rule and applies the following:
    • If it is required to have a driver’s license and the driver has a driver’s license, nothing happens
    • If it is required to have a driver’s license and the driver does not have a driver’s license, increase the ticket price
    • If it is not required to have a driver’s license and the driver has a driver’s license, do nothing
    • If it is not required to have a driver’s license and the driver does not have a driver’s license, do nothing
  6. Use the print() function to print the correct sentence. Refer to the software description for the required sentence.

Execute your script and confirm that it works as described in the software description. Try to fix any errors that could show up in the Output frame. An example answer to this exercise can be found on the GitHub page for this book:

https://github.com/PacktPublishing/Mastering-Roblox-Coding/tree/main/Exercises

You have been reading a chapter from
Mastering Roblox Coding
Published in: Aug 2022
Publisher: Packt
ISBN-13: 9781801814041
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image