Keeping it simple and clear. How would I fire a RemoteEvent in an ModuleScript?
Example: If i would’ve fired a remote event for the client the output would error that FireClient can only be used on the server the same goes if you are also firing the server except that it errors that FireServer can only be used on the client.
How would I fix this?
Would the alternative just be sticking to bindable events? Since I don’t like using these in modules just as an alternative for this problem.
2 Likes
You fire RemoteEvents the exact same way as you do in any other script. If the ModuleScript is called by both the server and client, then FireClient
and FireServer
won’t work for the incorrect context currently running the script.
You should check if the ModuleScript is running under the context of the server or client before calling the methods.
-- Example
local isClient = game:GetService("RunService"):IsClient()
if isClient then
-- ... called by a LocalScript
else
-- ... called by a ServerScript (or just 'Script')
end
https://create.roblox.com/docs/reference/engine/classes/RunService
https://create.roblox.com/docs/scripting/scripts/modulescript-patterns#example-network-module
2 Likes