Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do you send and pick up something from Client to Server?

Asked by
Scaii_0 145
5 years ago
Edited 5 years ago

So with FireServer(), I know you can tell the server when to do something. But can you also send information, like values?

So what I mean is:

local value = script.Value.Value
local value1 = script.Value1.Value

RemoteEvent:FireServer()

How would you send those two values to the script that is picking it up? And how would the Server script use that information?

2 answers

Log in to vote
1
Answered by 5 years ago

How would you send those two values to the script that is picking it up? And how would the Server script use that information?

You've already answered most of your questions. To piece everything together, pass your values through the :FireServer() function, then set-up an event listener on the server to receive the sent information, like so:

Client

local value = script.Value.Value
local value1 = script.Value1.Value

RemoteEvent:FireServer(value, value1)

Server

RemoteEvent.Event:connect(function(value, value1)
    --do stuff
end)
Ad
Log in to vote
2
Answered by 5 years ago
Edited 5 years ago

Short Answer: Yes. However...

This is a great question because there are some inner complexities that some people are unaware of. Let's start with some information about functions. When you call a function in your code, there are a lot of special conditions which allow that function to be called. These conditions require something from your computer, such as memory. This makes the function incredibly powerful, and allows you to do things such as: pass arguments to the function, return something from the function, scope variables in the function, etc.

Why should you care about this? Well, because FireServer is a function! Therefore, it inherits all of the characteristics of functions explained above. So yes, passing arguments to FireServer on the client and receiving those arguments on the server is entirely possible. But not for the reason you may think...

Encoding

Think carefully about those "special conditions" which make functions so powerful. All the information you need, primarily memory, is stored locally on your computer. So, what if you wanted to invoke a function on another computer, from your computer, over a network such as the internet? Welcome to the client-server model. This process is no longer as simple as just calling the function from your computer. Now you're transmitting data, which must be properly encoded before being sent off to the recipient computer (the client or server).

Luckily, for primitive data types such as numbers, strings, and booleans, encoding this information is incredibly easy and (hopefully) all encoders and decoders should have no problem handling this. What you send on one end will be exactly the same as what you receive on the other end.

Events and Event Listeners

You should also note that this is no longer just one direct function invocation over a network like I made it sound. Instead, calling a function such as FireServer will send data to a server-side event listener called OnServerEvent, where it will then process the event and call a new function on the server with the parameters

So, What's the Problem?

Objects. Objects are the problem. Just to clarify, an object in programming is an abstract concept. Tables, functions, variables, etc all count as objects; they're problematic because unlike the previously mentioned data types, these objects have to be broken down, or serialized before they can be transmitted. Because of this, what you send on one end might not be exactly the same as what you receive on the other end. Some attribute of the object could fail to be properly encoded due to having some reference to your computer, which the recipient computer does not have access to.

Why is This Relevant?

This is relevant because everything in your game is an object, including the data types mentioned before. Passing any of these values to FireServer could produce unexpected results if you're not careful.

Conclusion

Try to stick with passing primitive data types to and from the server. Not only will it save you the trouble of worrying if an object has been transmitted correctly, but it also forces better coding habits and cleaner code. Here are some more rules-of-thumb that you should remember when passing arguments through remotes in your game:

  • You cannot pass a function as an argument

  • Tables are fine as long as they don't contain anything that references something in memory. Stick with literals only.

    • Example: Use {1} or {v = 1} rather than v = 1; {v} or v = 1; {v = v}
  • Tables won't keep their metatables when passed as arguments

  • Passing instances will fail unless the instance is recognized by the server

This answer was probably way longer than it should have been and if so, I apologize that. But hopefully, this bettered your understanding of how data transmission, functions, and events actually work.

Answer this question