Is there a service that both client and server scripts can run in?

I have nothing really to say other than the title, but I guess I have to.


So basically I’m making a script that can detect if a message appears in the output appears that is either on the server or is on the client, but I don’t know what service I should put my script into.

Very basic server script, inside of workspace for now:

coroutine.wrap(function()
	game:GetService("LogService").MessageOut:Connect(function(Message)
		game.ReplicatedStorage.CurrentLine.Value = Message
	end)
end)()

Would I have to use remote events, since the script is a server script?

Any help is appreciated!

If there was a service that could act as a server in the client and vise versa it would basically be breaking the client-server model boundary (You can think of what a exploiter would be capable with this), as for the question at hand I believe you will need to use remote events for communication, for the server and client needs.

Should I try to use a ton of remote events to bypass this? Or should I try something else?

EDIT: I thought that a service like that would be stupid, I just wanted to make sure. Also, My situation probably can be fixed with remote events, it’ll just be a lot more complicated.

You can check what environment you script is running on with the help of the RunService:

local RunService = game:GetService("RunService")

if RunService:IsClient() then
    -- Client side
elseif RunService:IsServer() then
    -- Server side
end

From there you can create a type of Module Script which creates a Remote function/event and gets the given callback connected to the event via connection or function.

Example:

local Library = {}

-- Server side only:
function library:AddFunction(name, callback)
    local Remote = Instance.new("RemoteFunction")
    Remote.Name = name
    Remote.OnServerInvoke = callback
    Remote.Parent = script
    return Remote
end

-- Client side only:
function library:CallFunction(name, ...)
    script:WaitForChild(name):InvokeServer(...)
end

return Library
1 Like

I was able to slap together a pretty hacky solution with a single extra remote event, the scripts for the project I am currently working on don’t need to be that clean, I’m mostly making it for fun. Speaking about my project, the script I was working on is basically to fix a but with my custom built in functions for a command line game, where you can just write code, but it was a little boring so I decided to add some extra functions. Thank you for your help!