Intro to Remote Events & Functions (and Sanity Checks)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
do you want to learn how to use remote events and functions to communicate between the server and the client or maybe you want to learn how to prevent your game from getting exploited using these very powerful remote events and functions if you fall into any of these categories or just want to learn something new in roblox stay tuned and watch this video [Music] so before you use remote events and remote functions you first need to know what they do and how they work so basically the main purpose of these functions and events is to communicate between the server which is where like scripts are run and the client which is each player and that's where like local scripts and like the player character is simulated and whatever so you can do that with either remote events or remote functions and prior to filtering enabled you didn't really need this but since filtering enables a thing where basically the client can't really edit whatever the server sees unless the server like lets it happen we have to use remote events and remote functions which is good so people can't just exploit your game to oblivion so let's start with remote events a remote event basically sends a signal from either the server to the client or client to the server i'm drawing right here or the server to the client there you go so it's one way so you can send you can send a signal that just tells it to do something you can also send like parameters with it depending on like what you want to do and that's good but that only works if you want a one-way connection if you want to send something if the client wants to send something to the server and the server wants to give something back to the client in order to like verify it that's when you use a remote function so let's say the client sends something to the server the server processes it and in the end it sends back another value and this is exactly how a function a non-void function would work in lua so you could have a function like here and you could return something and that's exactly how it would work but instead of a normal function it'd just be a remote function so i'm here in roblox studio and let's get started so first thing you want to keep in mind when you're making your remote event events and functions is where to put them so you want to put in a place where both the client and server can see it so a good thing to keep in mind is that server anything with server like service script service or server storage client can't see that the server can technically see inside each player but that's just a bad spot to put it because it'll be replicated across each client and you'll have to work with each client individually so that leaves workspace replicated first replicate storage so you could put your stuff in workspace but i like to reserve that for parts and other things that will like appear in the world replicated first just goes from the client into the like it replicates into the client originally so that's not really a good spot either visa only does it once it won't really continue replicating replicated storage is the best candidate so i'm going to go and replicate storage create a remote event and let's also create a remote function and then we can just deal with these so i'm going to go into startup player starter player scripts and create a local script let me zoom in a little bit let me get our remote event which would be equal to game dot replicated storage let's just do a wait for trial just to be safe remote event and then our remote function which is game dot replicated storage wait for child remote function there we go and i'm going to do the same thing in server script service but instead of a local script i'm going to create a script and just to make it easier i'm going to copy all of these variable declarations and just shove them in here and a good indicator that the client or the server can't see your stuff is if you can't copy the lines and it'll still run so like they're both looking in replicative storage they both can see replicated storage so we're good so we're going to start by sending a message to the server i'm on the local script right now i'm going to say remote event fire server now this is the only function that allows you to fire the server on a remote event and this will basically just send the server and you can also put whatever parameters you want so this can be just like to print a message on the server let's say so remote event dot on server event connect and we have a function here so the first thing you want to know is whatever parameters you send it's going to be offset by another parameter it always whenever you're sending from the client to the server it'll always send the player who sent it first so no matter if you use this variable or not it will always be the player variable first so that could screw you up really badly if you don't know that because if for say i say message which would be like hello it would actually be player and i try to print out message it just print my player's name and i'd be very confused so put player comma message because you always get the player sent so and now we can print player.name says message there we go so if we run this your ic763 says hello that's exactly what you want so now if we want to do that in the reverse direction you can say remote event fire client or we could say remote event fire all clients so fire client basically fires a single client and it'll take in the parameter of the player that you want so let's just do fire client whoops game dot players get players one so let's just just get the first player actually let's wait a second because we have to wait for the character to load in and then we'll get the player and we'll fire the first player which should be me since i'm playing a single player so we'll do remote event dot on client event so you notice that syntax is the same other than saying on client and on server which is good so on client event connect a function and the client does not get any default parameters so we can just print client was called and there you go so that's the basics of remote events so you'll notice that this is a one-way single so if i were to return one or like return a value no one would get that like the remote event would not pick that up it would not capture that and return it that's where remote functions come in so now let's access our remote function so i'm just going to firstly comment out these lines of code real quick and they change the shortcut it used to be ctrl shift c but now it's control like slash and you can obviously change that in the settings but i just noticed that because that's more in line with like visual studio but that's a tangent okay so let's get back to remove functions so we have our remote function and the idea is very similar the s the syntax is very similar but slightly different and those differences will get you if you ever are unsure or confused honestly it happens to me a lot they're both so similar just look up the documentation that's what i always do and it works really well so first thing we'll do we're on the client and we're going to invoke the server and we're actually going to capture our value we're going to say local server value equals remote function invoke server and now on the server we're going to say remote function dot on server invoke don't connect we have to say equals so we're basically setting a property of our remote function and this property is a function property that may or may not return a value and most of the time it should return a value if it does not return a value use a remote event so i have a function and it obviously gets the player so we can return 10 or something and so we can print server value and it prints 10. and a very useful you like this on the surface and may not seem that useful you could maybe get a constant like define a constant here say local constant equals 10 and you could return a constant so that only the client the client can get that value it can't actually change it on the server but one of the biggest uses for this is some like client-side verification so let's rename this value to success and this will be a boolean value success so on the server script we can basically say like do like a transaction for example say um like if true you'd obviously change this then like do something so if the player if some action is true then we'll do something and we'll have a variable local success equals false so if this is true then we set success equal to true and regardless we return success so basically what would happen here is like let's say the player wants to buy something so what will happen is we'll get the player we'll check here presumably if they have enough money then we'll set success to true and will return success so the client will know if their transaction was successful and they'll know it immediately which that is a very good use and that also brings up another good point and that is sanity checks so we're going to do a basic sanity check right here so i just quickly set up a simple example of when a sanity check would be useful and insanity check is basically when you're checking if your variables make sense and they usually occur on the server so i have this in my main script i basically set up some leader stats real quick and then i have the on server invoke function that let's say they want to buy something so they take the money value and subtract it by the cost but if i were to run this right now like if i send in a cost value of like a thousand we'll see what happens so it gives me negative money which that's not good and by the way it's negative 900 because i started with a hundred money originally and if you don't know what this is uh just look it up leaderboards and leader sites are pretty simple if you want i'll make a video on it but back to here so this we need a sanity check right here and basically what do we want our sanity check to do we want to check if our player is able to buy this so we can just say if money dot value is greater than or equal to cost then we can subtract the money value so you'll see if you run this now it doesn't do anything and also to couple with this we could add our success flag variable success which will originally equal false and then we'll set success equal to true but if we're not successful we'll set success equal to false and i'm just doing this for readability it's already full so you don't really need it but basically this could be used to like make a little buy system so you could print success so this won't be successful so it'll print false but if you want to buy something that's let's say 50 money it will be successful and it'll subtract our money so just add like so you can maybe do a gui pop-up that says transaction successful minus 50 money or if you're not successful you could just say like transaction failed you do not have enough money or something like that so this is a really basic example of sanity checks and don't get me wrong it will not always be like this depending on your variables you might need to check the type of a variable you might need to check a like value or a certain property but the biggest thing to keep in mind is never ever trust the client and the reason you never want to trust a client is for things like exploits so an explainer can create a local script and it can look through replicated storage find all the remote functions at remote events and fire them let's say with an arbitrary value of like hundred thousand or something so let's say you have a a remote event that says like add money and you don't check at all for the client so x wider could basically just add like millions and millions of dollars to their money without doing anything just because they're calling one of your unprotected remote events so you need to add these little if statements and make your code very defensive meaning that like it doesn't let errors through but it doesn't actually air it just catches these errors before anything else can happen so you can even put like a warrant statement for debugging say like transaction not successful so this would be good for debugging or something like that and that's about it for this video so i hope you guys enjoyed and if you want i use a little different method for my remote communications and it works pretty well i use like a command system so if you want to see a video on that then i will make it just comment down below but i hope you guys enjoyed this video is kind of simple but it's a good base for any like remote functions or remote event use and i hope you enjoyed the extra sanity checks part that would be very important especially if you have a good game that needs to be secure because it has a lot of players and more players brings more exploiters and people with mal intent so i hope that helps and other than that make sure to like and subscribe if you enjoyed i hope you have a nice day and goodbye [Music] you
Info
Channel: B Ricey
Views: 4,509
Rating: undefined out of 5
Keywords:
Id: Zh-Jd4xEc2g
Channel Id: undefined
Length: 16min 6sec (966 seconds)
Published: Mon Dec 21 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.