What runs first, the event or the stuff written before the event?

(Imagine this like the first player just joined)
Would “test1” print first, or “test2”? Or does it depend on how fast the player loads in?

local plr = game:GetService("Players")

print("test1")

plr.PlayerAdded:Connect(function()
    print("test2")
end)

Stuff written before the event obviously
Althrough i suggest you to connect event first and run code below that

Etc:

local function PlayerHandler(plr:Player):()

end

Players.PlayerAdded:Connect(PlayerHandler)
for i,v in Players:GetPlayers()
PlayerHandler(v)
end


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)

What if print(“test1”) was after the event thing?

You seem to not understand how evenets work at all…
There is thing called defered events…
Look at code above i sent

You better learn how roblox handles events and not doing same weird shananigans driven by your paranoia.

Hmm… Probably I just don’t understand.
I just checked that when I put print(“test1”) after the event, test 1 still prints first.
That’s normal, right?

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 :joy:

Yep, thats normal.

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.

1 Like

The event is made after you print “test1”, that’s why it prints first, you can just playtest that in Studio.

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.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.