RoGo Magazine - Guide to Filtering and RemoteFunctions/Events

.

Posted in Tutorials

August 27, 2014 at 7:07 PM

Guide to Filtering and RemoteFunctions/Events

By Aqualotl

Editors: Joshuats

I’m going to start off this post by saying that this tutorial is for the more advanced programmers. If you are just beginning to learn Lua, you may want to stick around and wait for some of our other tutorials before you come to this one.

“Why are you writing this then, Lion? :c” some of you may be thinking. Well, I’ve been hearing a lot of chat in the developer’s community with people saying filtering is pointless and confusing. I, for one, was much like this. However, within 10 or so minutes, I figured out filtering and I found it very easy to use. So, I am here to write a tutorial on how to properly use filtering.

Let’s begin with the basics. What is filtering? Filtering is a feature ROBLOX added to help further prevent exploits. When enabled, it will ignore any changes created by a LocalScript. It will also ignore any changes made by the client. Here’s an example in images:

Here is the server and a client:

image0

Let’s say the client wants to call the server a potato. Without filtering, the server would agree and make changes.

image1

However, once you enable filtering under the Workspace’s properties, like so:

filteringenabled

The server will ignore anything the client says to it and changes will not be made.

image2

Another thing to mention is that anything the client changes WILL change just for that client. So let’s say the client calls the server a potato again. The client would SEE the server as a potato, but the server would not make the change and anyone else would not see this change.

image3

This feature is very useful in many ways. Not only is it perfect for preventing malicious exploits, but it can also be used for good. A common use for this is to create local parts–parts that show up just for the client. In the past, this was achieved by placing the part in the client’s camera. Filtering makes this much easier.

So, now that I’ve explained filtering, I will begin to talk about how to use it. First, as previously mentioned, the server will ignore anything the client tells it to do. This means everything you want the client to do must be controlled via the server. This can be achieved with RemoteEvents or RemoteFunctions. As a very simple example, let’s say we want to change a part’s BrickColor, that is located in Workspace, through a TextButton in a ScreenGui, inside the player’s PlayerGui. I’m assuming you know how to insert these objects and I will not go over it. If not, this may not be the article for you. :P

Inside ServerScriptService (best place to store sever scripts since they cannot be seen by the client), you would place a script. Now, here is where you need to make a decision whether to use a RemoteEvent or RemoteFunction. Once the client fires a RemoteEvent, it will continue on with the next task given by the code. RemoteFunctions will wait until the server finishes the task before the client will continue on with the next task. In this case, I will be using a RemoteFunction, but it can easily be switch out with a RemoteEvent.

Inside the Script’s source, you would put the following:

local RemoteFunction = Instance.new("RemoteFunction", game.Workspace)
RemoteFunction.Name = "ColorChanger"

function RemoteFunction.OnServerInvoke()
	if game.Workspace.Part.BrickColor == BrickColor.new("Bright red") then
		game.Workspace.Part.BrickColor = BrickColor.new("Bright green")
	else
		game.Workspace.Part.BrickColor = BrickColor.new("Bright red")
	end
end

What this will do will create a RemoteFunction in Workspace called “ColorChanger”. This Function will toggle the Part’s BrickColor from Bright red to Bright green whenever it is invoked. Now, let’s make the client invoke this RemoteFunction whenever they click the TextButton. Add a script to the ScreenGui with the source of:

script.Parent.TextButton.MouseButton1Down:connect(function()
	game.Workspace.ColorChanger:InvokeServer()
end)

This short function will invoke the RemoteFunction and fire the function “attached” to it. This can easily be manipulated to fit the needs for any games you are developing. Another helpful tip is that you can send data over to the RemoteFunction from the client. In the script inside ServerScriptService, simply change:

function RemoteFunction.OnServerInvoke()

to:

function RemoteFunction.OnServerInvoke(player, message)
print(player.Name.." says "..message)

and in the client, change:

game.Workspace.ColorChanger:InvokeServer()

to:

game.Workspace.ColorChanger:InvokeServer("Sup, server!")

This will cause the server to print the message “[PLAYER's NAME] says Sup, server!” every time the player clicks the TextButton. Please note that the Player object is sent over to the RemoteFunction automatically as the first argument. The client does not need to send it manually.

I’m hoping that this article will clear some things up with filtering and can help many developers convert over to it. I see it as a great way to prevent exploiters and it’s honestly not that hard to use. If you have any questions, please comment below and I will try to answer them.

Here are some wiki articles that I found useful:

RemoteFunction and RemoteEvent Tutorial

FilteringEnabled