In this post, I'm going to teach you the basics of coding (or scripting) for your games in Core.
The guide is written in such a way, as a piece of content that I wish was available to me while learning LUA for my Core Games.
This guide will be updated as my knowledge increases and I find better ways to explain concepts.
Let's get started.
The Basics of Lua
To be able to understand the Core API we need to learn a few basic concepts first.
I suggest firing up the LUA Demo so you can test out the different code blocks we'll be discussing.
Variable is a term used in algebra. In simple terms, it's naming a piece of data.
variableName = value
In the above example, variableName would reference whatever it's value is.
variableName = 1
By giving variableName a value of 1, simply writing in our code variableName would then be the same as type 1 in our code.
Variables are extremely useful as it allows us to change variableName's value and then use that new value in future code.
variableName = 1 variableName = variableName + 1 variableName = variableName + 1
In the above example, we first define variableName as 1 on line one.
Then online two, we take variableName = which means we are redefining variableName and we are taking it's old value and adding +1. Which means variableName would then become 2.
Finally, we do perform the same equation once more and variableName ends up being defined as 3 on line three.
Numbers or Int for integer in Lua allows you to perform equations on them.
num = 1
A string is anything you want to access as "text". Simply wrapping your value inside of quotes " " will turn it into a string.
string = "1"
In the above example, our Variable still is equal to 1 but it is no longer a number, it is a string, so we no longer can perform equations on it.
A float is a number that includes a fraction represented in decimal format.
float = 1.05
A boolean can either be true or false.
readingThisGuide = true
In the variable above, we set readingThisGuide to true. This allows us to perform different checks throughout our code to only run if certain variables meet certain conditions.
If clauses allow us to check if certain criteria are met, before running the code within them.
readingThisGuide = true if readingThisGuide then print("You are reading this guide") end
In the example above, if readingThisGuide is equal to true, then we'll print "You are reading this guide" if readingThisGuide is equal to false, the code will do nothing because it failed the if check.
We are also able to perform a specific piece of code if our "if condition" is not met.
readingThisGuide = false if readingThisGuide then print("You are reading this guide") else print("You are not reading this guide") end
In the above code, our readingThisGuide is now defined as false, so it will fail the if condition and instead perform the code within our else statement and print "You are not reading this guide"
If conditions can perform any check on your various data types and if they result in true, it will then perform the code block directly under it.
However, if the check results in a false and you've defined an "else" statement it will then perform the code in your else.
Here is one more example, using numbers instead:
Num = 1 if 2 > Num then print("2 is greater than Num") else print("2 is less than Num") end
We defined the variable Num as 1. We then performed an if clause checking if 2 is greater than 1. This results in true, so it prints "2 is greater than Num".
A while loop is similar to an if statement, in regrades to it running code if it's true.
However, as long as whatever condition you're checking remains true, the code will contentiously run.
Num = 1 while 2 > num do print("2 is greater than Num") end
The code above, since 2 is greater than 1 the code will continue to print "2 is greater than Num".
A simple example of a while loop finally end
local Num = 1 while Num < 10 do Num = Num + 1 end
The above code will continue to run while 10 is greater than Num. Inside of our while loop, we are adding + 1 to Num, so the while loop will execute 10 times before Num is no longer less than 10. You'll also notice on line 1, we added local to our in-front of our variable we'll get into why we added this.
In LUA, the scope defines where variables and functions can be accessed or "seen". Each block of code can access any local variables or functions within their parent or themselves, but cannot access those found in child blocks.
local reading = true function read() reading = false end read() print(reading)
In the above example of inline 1, we are defined as a variable that can be accessed throughout the entire script. Inline 3 we are using the global of "reading" which is the variable on line 1 allowing us to change it in the child block.
local reading = true function read() local reading = false end read() print(reading)
Similar to our previous example but this time we are defining a new local variable on line 3 while line 1 will remain untouched from the child block. printing will return true.
When first starting out creating your first script in Core, it's very important to understand Scope.
An example of how this can cause a problem for you in Core is being able to access different objects, for example, the player object from one function to another, even if it's in the same script.
function OnPlayerJoined(player) player:AddResource("xp", 100) end function OtherFunction(player) player:AddResource("xp", 25) end Game.playerJoinedEvent:Connect(OnPlayerJoined)
In the example above, our OnPlayerJoined function has access to the player object, because it was passed to that function from our Game.playerJoinedEvent.
Game is a global object that we can access within our scripts, which will return different data back to us, depending on our request, usually through events.
We'll get into how events work a bit later, but what you need to understand, for now, is that the event calls our function and passes us which player joined the game. This gives our function the player object when this a player joins a game.
However, our "OtherFunction" player hasn't been passed anywhere, so the player will be invalid. Even though the player was defined in the same script, it was defined in a child block of code which can only be accessed from that function.
There are ways we can access a particular player in our "OtherFunuction", which we'll go over how to do shortly. But first there are a few other gotchas that we must understand.
function OnOverlap(trigger, other) if other:IsA("Player") then print(other.name) end end trigger.beginOverlapEvent:Connect(OnOverlap)
Other events can pass back more than just a player. The reason for this is, triggers can be "fired" when objects other than simply players interact with them. For example if we were to have a trigger in our game, that we wanted to print the players name, a player interacted with the trigger, we would have perform a check first.
if other:IsA("Player") allows us to check the "other" object to verify that it's a player before running our code. If this is true, then other is a player, so instead of printing (player.name) we would instead print(other.name) as other was the variable passed, which holds the player object.
I know this can become confusing, as it definitally took me a bit to comprehend this myself. But once you understand what is being passed and how to filter that data to find what you are looking for, in this case finding a player as opposed to any other object, you'll be able to create a lot of cool stuff.
function OtherFunction() for _, player in pairs(Game.GetPlayers()) do if player == Game.GetLocalPlayer() then print(player.name) end end end
By creating a for loop, we are able to sort through all of the players in the game, by calling Game.GetPlayers(). We then create an if statment that if the player matches Game.GetLocalPlayer() then we print the players name.
Note: that Game.GetLocalPlayer() can only be called from a script running in a Client Context.