Remote Events Explained - Roblox Studio

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey folks welcome back name code here and today I wanted to talk about something called remote functions remote events now if you've been developing on roblox at any time chances are you probably come across these terms like server scripts local scripts server-side client-side filtering enabled and even these remote events which were mainly gonna be talking about today now what does it all mean and essentially what it comes down to is the server is what runs the game and the client which is the players that are playing the game on their local computers or phones whatever they download from the server and replicate whatever it is that's on the server now up until a few years ago whatever happened on the client was automatically replicated back on the server so you could create a part and that be created across everyone else playing the game now what change when roblox brought in this thing called filtering enabled is if you wanted to send a request to the server then it was then filtered and the server checked and if you tried to create a part for example it would say no you're not allowed to do that so there were a lot of restrictions placed on what you could do from the local client device now filtering enabled is actually a property of workspace so if we go to you workspace a and we scroll down we'll see filtering enabled is ticked by default now on all new places you create creating sites to do filtering enabled will be turned on and all the games you play will have this enabled you might see old places that from a few years ago they'll say there'll be a little message it says the game might not function as expected this is because the older games were created before filter enabled came in and as it says the games might not work correctly I'd say probably the most common type of event you're going to come across is client to server so I've got this little place I've got set up it's a bit like a painting game I think and I've got of this town here we've got a paint GUI with a button that you can click and I've also got a paintbrush tool in the starter pack so go in and play the game and I'll present to you a problem here if you will so you can click this boy in here to paint the town red I'll turn all the houses in the town red and I've also got this paintbrush tool that we can whip out and whenever I click it's going to turn a random part in the town a random color so I can keep clicking it and I'll get a bunch of random colors appear and I get this beautiful work of art like so but if I click at the top here it says I'm currently on the client which is the player that I'm playing us but I can switch over to the server I don't notice that the houses are still gray now the game is still winning here's my character and on the client if we switch back the houses are still or they're jazzy colors but on the server the changes haven't taken effect now why is this well the script for my GUI is a local script you can see it here button local script and if you remember those filtering rules we talked about earlier well we're making the changes locally but they're not going to take effect on the server that's not allowed if we want them to make effect on the server we've got to use something called a remote event so what I'm going to do is I'm going to add in a server script I'm going to put this on the server script store it service area and go adding a script and I'm going to create a new event it's going to be we'll call it local paint paint red event and that will be equal to instance dot new will want a remote event and then we'll give it a name so we'll say paint red event dot name equals and we'll just make it the paint red event and then finally we need to give it a place of you know you can place things in the workspace or the server storage but for remote events as you want them to be accessed both on the client and on the server we store them inside the replicated storage so we say dot parent equals game dot replicated storage as its replicated on both the client and the service is it perfect so it created the event here and then we need to go over to our button local script again and instead of trying to make the changes inside the local script we're going to get rid of that control ax and we're going to paste it into a function on the server script over here so we'll say local function paint red and inside here we can do the painting red and then we are going to call this function whenever that remote event is triggered so we can say paint relevant dot on server event Connect and then the name of our function which is paint red now we need a way of triggering it now so we go back into our local script and then here it's just a case of triggering that event so we'll start off by finding it if you can remember the name it was a paint red event so I'll quit variable or say local paint red event equals game dot replicated storage and then seen as we're creating with instance new we're going to use the wait for a child so we'll just wait for a fraction and make sure it's being created before trying to index it immediately so paint red event and then when they click all we want to do is we want to send the message to that server so we say paint relevant : fire server it's a function so we have two brackets like that and so whenever we're gonna fire the service with sending a message off using this event and then over in the service script this event when when the serve event is triggered then it's going to call the paint red function which is here so we'll go ahead and test this now shall we win the game the town is all grey click paint town red houses turn red we'll switch over to the server and the houses are also red fantastic and of course I can do the same for my paintbrush script by creating a server and a local script so if we go back and check that you can see our paint bush is now going to take effect on the server as well perfect now server to client communication is useful if you want to reveal something to one player now you can see I've just gone ahead and created a little door here over the house and at the moment if you try and join the game well you're not going to be able to go into that house but let's say I wanted to reveal it to an admin so what I do is I go ahead and create a starter player script it's a local script of course remember local scripts County one in a few areas such as inside the started player or start GUI and will create a function here we'll say local function open admin door and what this function will do is it will say work space dark door dot transparency equals 1 and workspace dot can collide equals false so you be able to walk through it and we only want to call this function if the player is an admin so equate a script on the server now we'll call this war an admin script and we're going to be firing off an event just like we did last time now if you remember before we were on the local script and we were firing the server well it's the same process but now just in Reverse so we started off by referencing the event and another way of doing it instead of creating an instance new you can actually do it inside replicated storage like this so we can click plus here and click remote event and I can name them at the event open admin door and now inside my admin script I can say local open and admin door event equals game replicate storage wait for child open admin door and then all I want to do is I want to check if the player is an admin so I'll just use my ID and we can get a function a little bit like that and we then we will just call this function so we'll say game dot players dot player added connect to this player added function so all this is gonna do is whenever anyone joins the game then it is going to check if that user ID is equal to this which is actually the name the ID site of known code which is my character and if it's equal to the ID if it's the admin then we want to fire the client so last time we fired the server this time we use that event and we say fire client and many differences that you have to specify a player so we're specifying the player which has that user ID and so then over in the local script then we've got this open admin door function and all we need to do is we need to run it whenever this event so we'll reference this event again copy and paste that we can paste that in and this time we'll say open app indoor event dot and instead of saying dot on serve event we say dot on client event can app and the name of our function open admin door so now we go and play named Kalos in and you can see there's no door so we can run into the house no problem and if we go and test and switch over to the server you'll see the door is definitely still there it's just not showing on that individual client so we can run in and out but another player came along wouldn't be able to get through that door obviously you can see that you went and changed the ID to add an action number in and they went back in and played and now I've got the door here and we definitely can't run through it and finally we have server to all clients and now you can use this if you want to send a message to all clients as it says and it's mainly useful for tour des nating games so what we could use an example here is I could have a GUI and let's say wanted to show a message whenever anyone joins the game so we'll create a GUI here and we'll add in a text label and we'll just paste this text over here make it a bit bigger so you can see it and we'll keep that blank for now so we'll go to replicated storage and we'll add in another event I prefer creating the remote events actually inside studio rather than doing it inside the script then it makes it easier to see what you've actually got what events you have in existence rather than trawling through scripts so we'll call this event player joined event and then over in our admin script as we called it it could have called this something else but it's clinical add in script scene as we already have this play added event we're just gonna add an extra condition to it so we've got the admin door event I'm also going to have the player joined event so we create the verbal for that player joined event and that's will be a over there in the replicated storage and then after you've checked if it's the admin will add a new line and we'll say player joined of and instead of saying fire client or fire server we can say fire or clients and brackets because it's a function again so then we can copy this line we can go into this screen GUI we can add a local script in and inside it will create a verbal for the play joint event I've copied and pasted it from the other script and then we're simply gonna say create function we'll say local function update text and we want to call that text whenever this event so we say player joined event dot on client event connect with the update text now in this example we actually want to send some data as well so in update text we'll have one floor player and then we will make script dot parent dot text equals player dot dot which allows us to concatenate or add together two strings and then in quotation marks how joined so let's go back into that admin script on the server and work says fire all clients inside those brackets we're going to add in the player babel so whichever player joins a game we want to send this who we still want to send the data or the verbal of that player to all the clients so let's go ahead and try testing that we'll click Play you ah we've got an hour there what we need to do is we need to say player dot name we can't pass the entire player object so just the name of the player and then tell them they've joined there we go it says name coders joined and I'm about to test this in a proper server so I could open up a local server with two players press Start and we've got player two is joined to play a would enjoin din first and then player two joined in afterwards so you can see it says player two is joined along the top ok and that brings us to the end of our tutorial on remote events hopefully you found that helpful and you can go away and create some awesome stuff with it thank you for watching and I'll see you in the next video [Music]
Info
Channel: GnomeCode
Views: 12,476
Rating: undefined out of 5
Keywords: roblox, tutorial, gnomecode, gnome, roblox development, lua, programming, scripting, programmer, how to script, learn to script, how to make a game, game dev, roblox game dev, roblox game development, roblox studio, 2020, client, server, local script, server script, remote event
Id: 3wkPekjQvKg
Channel Id: undefined
Length: 16min 22sec (982 seconds)
Published: Wed Apr 08 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.