Struggling with remote events and functions

Hi, I am currently trying to learn remotes, what is a good way to learn them. I still find them hard after several tutorials. Is their someone that also wants to include a example script of when someone touched a ui button.

2 Likes

Remote Events/Functions are like bridges between User and Server side scripts. First you need to understand what is Server and Client sides are. If you have Filtering Enabled and try to somehow change the workspace from script inside one player(like moving a part) Then this wont be seen by other players and only Parent player of the script will see the changes. If you want all the players see such changes then those changes must be done with server scripts (All scripts that are in ServerStorage or ServerScriptService are considered as Server side) For further explanation i recommend to read RemoteEvent | Roblox Creator Documentation

You have to rely heavily on that documentation if you want to learn to script…

2 Likes

Remotes are ways the client and the server can communicate with each other. RemoteEvents are a one-way communication, while RemoteFunctions is a two-way communication.

Either must be placed somewhere the client and the server can access, essentially ReplicatedStorage. Workspace works, but please don’t.

RemoteEvents are used when you simply want to do something after an event occurs, such as pressing a button:

local Remote = game.ReplicatedStorage.RemoteEvent
local button = script.Parent

button.Activated:Connect(function()
	Remote:FireServer()
end)

Similar to any events, you can use connections to run a function when the event is fired:

local Remote = game.ReplicatedStorage.RemoteEvent

Remote.OnClientEvent:Connect(function(player)
	print(player, "clicked the button")
end)

RemoteEvents have three methods:

  • :FireServer(), this is used on the client to let the server know that we’re calling a scope.
  • :FireClient(), used on the server to fire an event to a specific player’s client.
  • :FireAllClients(), used on the server, and fires the event to all clients.

And respectively, they have two events:

  • OnServerEvent: Fires when :FireServer() is called from a client.
  • OnClientEvent: Fires when :FireClient() or :FireAllClients() is called from the server.

RemoteFunctions are simply functions that run and return values when either side calls it. Since it yields, it’s not recommended to yield their respective callbacks, as the side that invoked (called) the function will also yield.

Essentially, they’re used to return values. If you know how returning values in functions work, it’s the same. Do note that if the function that was invoked errors, the other side will also error.

local RemoteFunction = game.ReplicatedStorage.RemoteFunction

local number = RemoteFunction:InvokeServer(1, 4) -- It works like a normal function

-- Server --
RemoteFunction.OnServerInvoke = function(a: number, b: number)
	return a + b
end

-- number = 5

Unlike RemoteEvents, you can only bind one callback to a RemoteFunction. So use it accordingly.

You can always read the documentation for RemoteEvents and RemoteFunctions.

Only use RemoteFunctions if you look for values returned from the server. Otherwise, use RemoteEvents if you just want to call the server to do something.

I just noticed this error I had here for over a year.
image

5 Likes

This reply helped more than all the tutorials lmao, than you so much!

1 Like

Would you mind linking me the tutorials you’ve seen? Would like to take a look to see why they didn’t help.

1 Like

I don’t remember which but mostly the youtuber was just unclear. And the official remote events page also didn’t work after copy pasting the exact code it gave the infinite yield error

1 Like

You can’t just copy the code from the documentation page, you have to put what you know to the practice.

If you don’t create the respective Remotes then the script will obviously yield forever waiting for it to exist (which never will because you didn’t create them).

That said, don’t just read. Write too.

1 Like

I know, I just tested it to begin with than I learn of the code and put it in use. So for example the code you send is usefull as a example, than I learn how te code exactly works. And after I understand the code I try to write my own code.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.