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

Can I use .OnServerEvent() from a ModuleScript?

Asked by 5 years ago

For example, considering the following code is inside a ModuleScript:

local RemoteEvent = Instance.new("RemoteEvent", game.Workspace)

RemoteEvent.OnServerEvent:Connect(function()
    print("Hello")
end)

Will it print "Hello" when I call :FireServer() on the RemoteEvent?

0
I don't currently have access to roblox studio, therefor I can't test it out. Le_Teapots 913 — 5y

1 answer

Log in to vote
0
Answered by
Amiaa16 3227 Moderation Voter Community Moderator
5 years ago

Yes, as long as you require() the ModuleScript from a server script.

A ModuleScript cannot run without another script calling require() on it. Once it happens, it runs on the same peer it was required from.

So if you require it from a LocalScript, it will act like a localscript, meaning you can access LocalPlayer, FireServer(), etc.

However if you require it from a server script, it will act like a server script, meaning you can use OnServerEvent event and replicate stuff to all clients.

(Note when I say "act like a __ script", I don't mean stuff such as lack of Disabled property in ModuleScripts)

Ad

Answer this question