-
Notifications
You must be signed in to change notification settings - Fork 1
Lesson 1: Getting Started
Functions represent a series of actions and are easier to call then putting down the same code multiple times. To declare a function:
function myAmaze() -- function makes it a function, myAmaze is the name it would be called by later, and the parentheses are for parameters, see next lesson.
print("Joined!") -- Print the text "Joined!" in Output. See Quick Notes for better print understanding.
end -- Ends the function, tells it where to stop, contain all desired function code before it.This code will not call the function, it will only create it and leave it for later use. You must declare the functions before using them, the order of calls is top to bottom. Next, we have an event, let's say when a player is added:
local PlayersService = game:GetService("Players") -- This code is required to give us access to the Players Service.
PlayersService.PlayerAdded:Connect(myAmaze) -- This will call the myAmaze function and if you check your Output you should now see "Joined!"An alternative to doing this same thing would be
PlayersService.PlayerAdded:Connect(function()
print("Joined!")
end)This script should be in a normal Script in ServerScriptService
Functions are nice on their own, but it's even better when you can convey information to them, using Parameters, they're the variables located in between brackets. So let's take the code from our previous lesson, and let's make it print the Player's name as well!
local PlayersService = game:GetService("Players")
function myAmaze(playerObj) -- Here we get access to the playerObj variable, which can be called whatever you want really (Read Quick Notes for Exceptions).
print(playerObj.Name.." joined!") -- We want to print the name, .. is the concatenation operator, it combines strings and variables
end
PlayersService.PlayerAdded:Connect(myAmaze) -- This event is automatically called with the player object in mind, nothing to re-declare.Loops are a very important part of coding, you may need to keep something running for a whole round
This is a for loop:
for i = 0, 10, 2 do -- Repeats an action, i is the number which is used originally, 10 is the number until which the loop is repeated, 2 is the increment for each loop. It will repeat 6 times, with the numbers 0, 2, 4, 6, 8, 10.
print("Your number is "..i)
wait(0.2) -- We wait here to not overload the game, not that it's required for printing, but it's definitely good practice just in case.
endAnother type of loop is the while loop. This could be to perform something whilst a condition is true or false or while a number is smaller.
while true do -- True is a boolean, it will always be true, while will only run with a true boolean, if you want it to be false, do "while not true do".
print("Haha")
wait() -- Yeah definitely wait here.
endModules are created within ModuleScripts which should be located within ServerScriptService. They're useful for redundant and repetitive functions that you may need to call at some point.
local functions = {} -- This will make the "functions" variable a table, which lets us store any amount of functions in it that we want.
function functions.Speak(name, text) -- "text" is the string that will be applied as a parameter in our main script. "name" is the name string.
print(name.." says "..text) -- Print for example: "RigidStudios says haha" if the text variable is "haha"
end
return functions -- This will return the functions to the script that required it.Within the main script we now need to require() the Module, to do this, we'll have to first get access to ServerScriptService:
local SSS = game:GetService("ServerScriptService")And next we get to require the module itself:
local module = require(SSS.ModuleScript)After having done this, we still need to call the function, and as it's in the table, it should be done as:
module.Speak("RigidStudios", "haha")Check your Output!