Is there a way to make let’s say only client that can fire that function, server can’t? Like let’s say I have a module. If using server side / normal script it won’t run but if using local script it run.
Is more like to detect if the module function get fired by LocalScript or ServerScript(Normal Script).
Well, that’s the case for ScreenGuis, or basically anything that’s client sided.
I’m pretty sure that you need to actually use events to exchange info from Client → Server, and Server → Client
No I mean I want to make module like this, so let’s say you use a module from a local script, it will print (“You fire this module from a Local Script”) and same, if you use that on Server Script the module gonna print (“You fire this module from a Server Script”)
Use this:
if script.Parent == “ServerScriptService” or “Workspace” then
print(“You are firing this from a server sided script.”)
else
print(“You are firing this from a client sided script.”
Ah so I need to make like that? i can’t detect if it’s server or client?
You can use RunService for this. It comes with alot of functions such as detecting if you’re running on the Server, Client and on many more things.
RunService:IsServer()
— Checks whether it’s running on the Server.
RunService:IsClient()
— Checks whether it’s running on the Client.
Simply use an if statement or the assert()
function and include the methods into them. For example, If you would check if it is running on the Client, You would use this:
-- Other Variables...
local RunService = game:GetService("RunService")
local IsModeClient = RunService:IsClient()
local function DoSomethingClientOnly()
assert(IsModeClient, "Can only run function on the Client!")
-- Other code...
end
Or you can use RunService, which says
RunService:IsServer() – Server
RunService:IsClient() – Client
Can I make like this?
On module
RunService_1 = RunService:IsClient()
RunService_2 = RunService:IsServer()
function module.test()
if RunService_1 then
print “You run this on LocalScript”
elseif RunService_2 then
print “You run this on ServerScript”
end
end
Then in Local Script I make
local module = require blablabla
module.test()
Then it will print “You run this on LocalScript” because it’s :IsClient()
Do i get the logical right?
Yes, That’s it! But i would recommend you using the assert()
function in-case you want to make a function only run at a certain side. (Server or Client) The difference between assert()
and just using an if statement is that by using assert, It’ll prevent the rest from running in-case the condition is not met. Otherwise, If you want to make the function/method run on different ways related to the Side which you’re calling it from, Then use the if statement.
Well honestly yes thank you, but with my way it can detect if it’s with client or server right?
Yes, You can - There’s no worries about it.
Also, If that was the solution for your problem, Ensure to mark my reply as the Solution, As if more people with the same problem finds this post, They’re gonna find the right solution for it.
Yup, thanks your answer is what I’m looking for.