test1 happens before test2, so… test1 would print 1st.
This is a ServerScript set up to get the player. This 1st part is getting all the players.
The 2nd step is getting the current player and should end with function(player)
local players = game:GetService("Players")
players.PlayerAdded:Connect(function(player)
print(player)
end)
Could go one step more and get the character.
local players = game:GetService("Players")
players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
print(character)
end)
end)
In your example you run function before connection event gets connected
Yes that is 100% normal
Luau Interpeter executes code line by line
We dont have quantum computers running roblox client yet
The event yields(waits) for the player to join, so “test1” will print first, as it takes time for the player to load in, and a server script starts up when a new server is created(i think).
events create a separate thread, each time, thats why even if you put code after an event, the code continues.
Here’s my take on explaining why the code behaves the way it does:
Lua reads code from the top to the bottom of the script. This means that if there are two pieces of code that should run at the same time, it will start with the code higher in the script, and finish with the code lower in the script. However, when events are binded to functions, lua knows to “listen” for something, and when that thing happens it will execute whatever function you have binded to it.
So for your code, lua will scan it like this:
nothing notable here, so we can move on.
local plr = game:GetService("Players")
i don’t see the event in front of me because im focused on the current line. I will print this.
print("test1")
Oh! this runs at the same time as test1, but I didn’t see it yet so I am just now running this. I will always listen to this event now.
plr.PlayerAdded:Connect(function() ... end
I hope this explanation helps you understand the way the lua interpreter orders things.
It will always mean this: scripts are always read left to right, top to bottom. The only difference from normal reading is that you have control of the logic flow, and there may also be lag to consider.
This is a case where two() is called before one() with the logic flow.
function one()
print("one")
end
function two()
print("two")
end
two()
one()
It is still following the rule as two() was placed above one() in the script.