Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Can I use .OnServerEvent() from a ModuleScript?

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?

1 comment

0

I don't currently have access to roblox studio, therefor I can't test it out.

1 answer

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.