📱

Get Our Mobile App

Take your business learning on the go!

Download on the App StoreGet it on Google Play

Local Scripts (Server vs Client) - Roblox Advanced Scripting #1

BrawlDev14:55

Transcription

Hey, thanks for stopping by! Welcome to the first episode of my Roblox Advanced scripting tutorial series. And if you haven't checked out my beginner scripting tutorial guide I made a few months back, then I would highly suggest you watch that one first before watching this one because we're going to be uncovering more complicated and more advanced topics in regards to Roblox scripting. So if you think you're ready, then you're basically on the right track to becoming a a top-tier Roblox developer.

And I figured for the first episode, I think it would be important to discuss about local scripts and the server-client model because you're going to be knowing about this model for basically the entire time you're going to be developing games on Roblox. So this is going to be a very important one.

So any Roblox game, regardless of whether it allows more than one player per server or not, follows a multiplayer model called the client-server model. And to understand a little bit more about the client-server model, let me turn your attention to to a drawing that was created by the Roblox Developer Hub. And I think this is a really great drawing. So, so let's go to that real quick.

All right, so there's two very important aspects to know about for the server-client model. And the first one we'll be discussing about is the server. The best way I can describe the server is it's basically a reserved section of any Roblox game that oversees the whole game itself. So if I just drag this along really quick, you can see that the server can be represented as the game itself, where you can see that there's three players that are inside of the game and the camera is directed away from any specific player. So it's kind of like an overseer, it kind of oversees the whole game and it controls everything that happens with the game. So that's essentially the server.

But now to describe the client side, a client is, in the easiest way I can describe, the player itself, each individual player. So we have one player that's over here, then we have another player, and then we have our third player that's over here. So we have three different players that all act as clients that are connected to the server, which oversees everything. The server is what controls synchronization among all three of the players that are inside of the game. So you can see here, one of the clients is one of the players, um, the camera is directed towards them, and they're seeing everything that's happening in the game from their perspective. And that's going the same way for these other two players.

So there's three important things to know about with the server-client model. And the first thing is replication. Whenever something happens in the server, it will be replicated to be shown in every single player that are in the server. If a player decides to move their character, character, then it will show to other players that this character has also moved. And replication is playing a factor in making sure that all the players are seeing exactly the same thing that's happening on the server.

Another important thing is transmitting data between the server and also the client. So usually, if we want to change something through script where it will be replicated only to one specific person and not be shown to other people, then we would use features like local scripts. And I'm going to be showing you how those work in a little bit. So there can be different ways of transmitting data. There can be from one client to the server, there can be from the server to one of the clients, or there can be the server sending data to all the clients in the server.

So now that I'm done explaining how this works, let me show you some in-game examples of how this would actually turn out.

All right, so I'm in a game in Pet Simulator X, and I'm being joined by my alt account that I just created. So there's one thing to note here that's quite odd between the perspective of my account and my alternative account. So on my alternative account, you can see that I haven't unlocked the next area in Pet Simulator X because I need to get 10,000 coins in order to, uh, buy the game to get to the next area. But on my main account, uh, I have lots of progress in the game already, and I already bought the gate, uh, to go to the next area. So from my perspective, I can see that the gate is not there. But then on my alternative account that's new, they cannot access the next area until until they get 10,000 coins.

Another example of local-sided features is user interface. So for instance, these buttons on the left side, you have hoverboard, teleport, auto settings. If I click on one of these, it'll show a popup of a little panel that shows all the places I can teleport to. Now, if I go to my alt account on their side of the screen, they can't see the popup for the teleport. It's unique to them only if they click the button. So my alt account can click the button to show the teleport. But then on my main, I can X out of this and open up the pets panel. And on my alternative account, it's still going to be on the teleport page. So that's another big example of client-sided gameplay.

All right, so now we know having a good understanding of the client-server model is really powerful. And it's something we're really going to have to take into account for anything we do when we're trying to develop something, whether we want to show something specific to a specific player or if we want to show something to all players, it can be replicated throughout, uh, the entire game. And so that's always just something that you should keep in the back of your mind.

But now, what I want to do is show you how to access these different modes when you're creating on Roblox. So if we hit test up here and then hit play, then our character should be joining into the game like normal as a client. So here's our character here, um, and in order to flip between what you see on the client versus what you see on the server, we're going to go up here, uh, inside of the test ribbon, ribbon as usual. Uh, there's going to be a monitor here saying current client. So this is the current perspective that we're seeing in the Roblox Studio window, which is our player right here. If we click on it, we can actually see that the perspective has changed to the server now. So our camera is now on the default location for the, the server when the game starts up. And as we can see, we can freely move around in this server much like how we make games in Roblox Studio using the studio 3D world looking, um, map. So if I click it again, then it should go back to the perspective of the player and we should be able to move around freely like so. So that's how you flip between the client and server perspective when you're creating a game on Roblox.

And to give you a little bit more of an understanding of what the perspective looks like between the two, so let's hit stop. And now what we're going to do is create our first local script on Roblox. We create scripts to be able to give it instructions and make it do what we want to do in Roblox. But there's going to be a lot of times where we want the things to happen only within a specific player and not have it be replicated onto the server. And this is where local scripts come in.

So on the right side, if you have your Explorer window pulled up, I assume you have, uh, if you don't have it pulled up, then you go to the View tab up here and then the Explorer and Properties tab should both be here. So let me just open both of these back up, um, and make sure you also have your output down here so we can see the results that we're about to print out. So I'm just going to right-click our output and then clear it, uh, so then let's create our local script on our Explorer. Let's scroll down a little bit and on our StarterGui, we're going to hit the plus sign and then we're going to search for a local script. And this is what it should look like. So let's click that. And then we see that it brings up the script editor. And this pretty much looks exactly the same as you would see when you insert a normal script. But this time, this acts as a local script. So we're not going to change anything here. We're going to leave this as print "Hello world." We're going to go back to our game, we're going to hit test and then hit play.

So then what should happen is that we should see in the output, uh, "Hello world" just like how we normally would in a, uh, regular script. But here's the interesting thing. If we hit current client and switch over to the server, we can see that "Hello world" is still here. But if you see this color-coded pattern here, you can see that there's actually a color here that's blue and then down here is a color that's green. So whenever you see an output down here where the color is highlighted to be blue, that means that this little line of code ran on the client and not the server. So green means that it ran on the server. But in this case, our local script ran on the client as soon as one player joined the game, that's when it decided to print out onto the output.

Let's now make something that will only run on the client side and not have it be shown on the server. So we're going to create a part where when a player steps on it, it will print out a bunch of stuff in the output, but only on the client. So with our script that we created, let's actually disable this, uh, we're not going to be using this for the rest of the tutorial. And let's insert a part. So let's go to Model up here and then let's hit Part to insert a new part into the workspace. So let's just drag this along here. Let's change the scale of it a little bit. And let's change the color of the part down here inside of Properties. And let's change the BrickColor to Yellow, for instance. Um, and now what we'll do is we'll go down here to StarterPlayer in the Explorer. So open this up a little bit. And you will see a section called StarterCharacterScripts. So let's hit the plus sign here and let's create a new local script. And then let's delete this code that's been given to us by default.

First, what we'll do is make reference to the part. We'll say, "local part = game.Workspace.Part." So that will make reference to the part that we just created. And now we'll add a touched event to that part. So we'll say, "part.Touched:Connect(function()". Let's also delete this last line here. Enter. And then we will say, "print" and then in quotations, we'll say, "like some random mumble jumble here." Okay.

So now let's go back to the game. We'll hit test and we'll hit play. Okay, so it looks like I got an error here, uh, saying that "Part is not a valid member of Workspace." What ended up happening was that the part didn't load in fast enough to catch that there was a part in the server. So, so we're going to have to wrap this around in a "WaitForChild." So let's delete "part" here. And after Workspace, we'll say ":WaitForChild" and make sure, uh, you have the case sensitivity correctly. Then we'll, uh, say open, close parentheses. And inside of here, in quotes, we will say "Part." So now this is going to force the script to wait until "Part" is loaded into the game so that we can actually run whatever is down here.

So let's touch this. And as you can see, on the client, the part is printing out all this mumble jumble random nonsense. This stuff isn't being printed out on the server. Now, it may not be as clear to you because the only thing we can see here is the color difference. Let's do something more visual so we can actually understand it more.

So let's hit stop. Go back to our local script. And instead, what we'll do is we will change the color of the part when we step on it. So let's delete this print statement here. And let's try to write a line that changes the color of the part. So we'll say, "part.BrickColor = BrickColor.new(" and then in parentheses right here, we will put in some quotes and say "Really Red." So now let's go back into the game and hit play so that we can see our expected result that has come about with our script.

So here we are. And once we step on this plate, then it should change the color to Red. So as you can see, the color has now changed to red. But if we change our client to the server, we can still see that the color is yellow because we created a local script that can only show what happens with this execution. When this specific player touches this part, it will only show it happening on their screen and not be replicated to every other player that plays the game. Like if you just imagine that there's going to be another player here, another player here, and then there's another player here, they're going to see that the part is still yellow even though you stepped on it and the color is now red. But only you see the part being red, not anybody else.

So that's pretty much that. So that's pretty much the basics of local scripts and the server-client model. I will be going more into detail about these about local scripts when we tackle tools that require the StarterPack, uh, and also GUIs when I start my GUI tutorial series and things like that. But this episode served as an introduction to local scripts and the server and client-sided models. So I think I've done enough to show that for this episode.

All right, that's going to be it for this episode. Thanks for watching, and I will see you in the next episode. Take care.