What is wrong with ReplicatedStorage requiring a module from ServerScriptService?

Alright. So there is this major issue on the local script where I can’t even require a module from the ReplicatedStorage as well when the ReplicatedStorage requires a module from the ServerScriptService when it is non-existence. Take a look at the three major codes below and see what we can change. Here are also the properties & Instances


Instances

  • A module from the ServerScriptService
  • A module from the ReplicatedStorage
  • A local script calling the module from the ReplicatedStorage

Module from Local Script

print(require(game:GetService("ReplicatedStorage").Module):GetServerGametime()

Module from ReplicatedStorage

local module = {}

function module:GetServerGametime()
   return require(game:GetService("ServerScriptService").Module):GetGametime()
end

return module

Module from ServerScriptService

local module = {}

function module:GetGametime()
   return workspace.DistributedGameTime
end

return module

I know there is something wrong. However, we can change the instances or the codes to get them synced. What would you do to get them synced?

The print must go from the local script:

14.00468209502852
2 Likes

If you can’t access the container from a localscript, you can’t access it by routing through a modulescript. You can’t access any items in ServerScriptService with a localscript that were placed there by the server, if you place an item there on the client you can.

To achieve what you are currently doing, you should use a RemoteEvent/Function, it will have an error due to a client’s ping.

1 Like

When using shared modules (modules that you need to use on both the client and the server), you always need to assume that you can only access things that the client has access to, i.e. nothing in ServerStorage or ServerScriptService. If you need to run a function on the server, and have a value returned from the server, consider using a RemoteFunction!

Well done! Solution is solved by two people, SebastianAurum and Darkmist101. I have successfully figured out how to do it on RemoteFunction. Great! :grinning: :clap:

@Darkmist101
@SebastianAurum

Just out of politeness I’d recommend checking the solution on either one of the posts that actually solved your solution, rather than your own :slight_smile:

3 Likes

Eh. This isn’t necessarily true. If you change the values of some variables depending on the environment it’s being ran in, you can have a shared module easily that incorporates the server AND client. You shouldn’t need remotes and unnecessary traffic unless you really need cross-environment communication.

local RunService = game:GetService("RunService")

local IS_SERVER = RunService:IsServer()

local module = {}

module.Example1 = IS_SERVER and function(arg)
    warn("s", arg)
end or function(arg)
    warn("c", arg)
end

module.Example2 = function(first)
    return IS_SERVER and function(second)
        print(first, second)
    end or function(second)
        warn(first, second)
    end
end

return module

--

local module = require("module")
module.Example1("foo") --> env_letter foo
module.Example2("foo")("bar") --> foo bar
2 Likes