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

How do RemoteFunctions work?

Asked by
Ekkoh 635 Moderation Voter
10 years ago

I want to import a ModuleScript using the require function. The problem is, the ModuleScript is in ServerScriptService, which is only accessible by server scripts. I would like to use the module inside my LocalScript but I can't access it, so I thought RemoteFunctions would provide me with a way to still retrieve the module but I'm not sure how I would go about doing that. I'd appreciate any feedback, and thank you!

2
Why not just put the ModuleScript in ReplicatedStorage, or somewhere similar? DataStore 530 — 10y
1
You could also just pass the AssetId of the ModuleScript to require. suremark 140 — 10y

1 answer

Log in to vote
1
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
10 years ago

As DataStore said, you can just put the ModuleScript in ReplicatedStorage and require it from there.

Additionally, if you rename the ModuleScript to "MainModule" and upload it to ROBLOX as a Model, you can require the asset ID of that model (the string of numbers at the end of the URL) from both LocalScripts and Scripts.

To actually answer your question, here is the syntax for RemoteFunctions:

--Script (ServerScriptService)
local func = Instance.new("RemoteFunction", Game.ReplicatedStorage)
func.Name = "Remote"

func.OnServerInvoke = function(source, message) --OnServerInvoke is a method of the RemoteFunction that can be set to a function by the user, in contrast to other methods such as Destroy and Clone.
--The first parameter here, source, is the Player associated with the Client peer that called the InvokeServer method of the RemoteFunction from a LocalScript, as you'll see below.
    print(source.Name .. " sent a message: " .. message)
    func:InvokeClient(source) --The first argument is the Player to receive the Invocation of the RemoteFunction. This method will call the OnClientInvoke method of the RemoteFunction on the given Player's client.
end
--LocalScript (StarterGui, runs in a Player's PlayerGui)
local func = Game.ReplicatedStorage:WaitForChild("Remote")

func.OnClientInvoke = function() --Note the lack of the parameter supplied when invoking the Client.
    print("Server received the message!")
end

func:InvokeServer("I'm the scatman!") --Again, note the lack of the first argument. Both are assumed to be the LocalPlayer; that is, the Player attached to the client the LocalScript is running on.

Comment on this answer if you have any questions.

0
Thank you! Ekkoh 635 — 10y
Ad

Answer this question