What are remote events and functions and why/when do u use then
Both RemoteEvents and RemoteFunctions allow communication between the server and client(s).
RemoteEvents do not return any values when fired but allow for code to be run on the server/client when something happens.
RemoteFunctions return a value that can be used by the invoking party (either the server or a client). They are otherwise similar to RemoteEvents.
As such, RemoteEvents should be used when you do not want a return value, and RemoteFunctions should be used when you do want a return value.
One thing to note: RemoteFunctions are invoked while RemoteEvents are fired.
Here's a brief video on remote events
Re-edited by JesseSong
There's a thing known as the server-client boundary in Roblox. Whatever the server changes in the game affects all players (clients) but the reverse is not true, this was created to help reduce the number of exploiters/hackers in games. Now, sometimes it is necessary to use stuff only available from the client (mouse input, keyboard input are both good examples) and make changes to the server.
For example, you press the shift-button and you want a fireball to shoot out of your arms. Now, if you scripted one using localscripts, no one would be able to see you shooting it; but the server doesn't have a physical keyboard so there's no way it can fire anything. The solution is to use RemoteEvents and RemoteFunctions.
RemoteEvents allow you to either send information to/from the server/client. RemoteFunctions allow you to send information AND get information back (this is called returning) from the server/client.
Here's an example usage of a remote event:
-- server script workspace.RemoteEvent.OnServerEvent:Connect(function(player, key) print(player.Name.." pressed "..key) end) -- local script game.UserInputService.InputBegan:Connect(function(input) workspace.RemoteEvent:FireServer(input.KeyCode) end)
And of a remote function:
-- server script workspace.RemoteFunction.OnServerInvoke = function(player, number) return 52 + number end) -- local script local add52ToThis = workspace.RemoteFunction:InvokeServer(203) print(add52ToThis)
Of course, these aren't the only ways to use both. There's an unlimited amount, and you will only be able to understand through lots of practice.