Is RunService:IsServer() Reliable?

  1. What do you want to achieve?
    I want to make a module script that both the client and the server can require and run their own functions from that module. But if there’s a variable that both of them use, for example an event, they can have it at the top without having to make 2 different scripts.

  2. What is the issue?
    I’m not sure if doing a RunService:IsServer() is reliable to protect the server’s function from a client.

  3. What solutions have you tried so far?
    I thought about making a different ModuleScript inside the server storage, but I’ll have to require 2 modules and type down the same variables again.


Basically, I’m working on a custom weapon kit (Which I’m not 100% sure I’ll release), and I want it to work like that:

  1. The client detects when a new tool object has been added into the backpack or the character.
  2. The client will fire the server and the server will find a module script that has the same name as the player’s tool inside the replicated storage.
  3. If found, the server will run the server function, return the module, and the client will run the local function.

The module looks something like this:

return function(Tool)
	--// Services //--
	local Services = {
		Run = game:GetService("RunService")
	}
	
	
	--// Variables //--
	
	--// Events
	local Fold_ToolEvents	=	script.Events
	local Event_Hit			=	Fold_ToolEvents.Hit
	
	
	
	--// Functions //--
	return {
		Local = function()
			print("Local!")
		end,
		
		Server = function()
			if Services.Run:IsServer() then
				print("Server!")
			else
				print("Cannot run server function from the client.")
			end
		end,
	}
end

I’m sure there’s a simple answer for that, thanks for reading :slight_smile:

I am not sure if you should expose the code to the client like that. Why would you not consider two different modules?

1 Like

I can actually pass the module script instance to the one in the server storage actually…
So nevermind, just didn’t think till the end :skull:
But would RunService:IsServer() even be reliable / useful?

Yes, it is reliable for checking whether the script is executing on server or not(I must have forgotten about the main question). Mostly useful for testing I believe, but not much into the real deployment.

Yes it would be, I use this method all the time if I don’t feel like repeating my client/server code. And even if the client modified the server code, it wont have any effect because the server has a copy either way. Only risk is if the client reads the code they can figure out how it works and try to break it

1 Like

Well there isn’t something that important in those scripts, only some event detections such as hit and probably other stuff.
But either way thank you for the answer!!