Note To Myself:

Post the following to Platform Feedback - Engine Bugs when the Post Approval ‘renovation process’ is finished.


Title: The contents of required on-site ModuleScripts do not replicate to clients

Thread: This module is (a testing duplicate of) an on-site ModuleScript that I often use in my server-sided code. It saves me from needing to write out a PlayersService:GetPlayers() loop along with connecting a listener.

The following code is an example of me using this module. It creates a ‘seconds’ counter on the leaderboard for each player:

require(5871065556)(
	function(JoiningPlayer)
		local Stats_ = Instance.new("Folder")
		Stats_.Name = "leaderstats"
		
		local Seconds = Instance.new("IntValue")
		Seconds.Value = 0
		Seconds.Name = "Seconds"
		Seconds.Parent = Stats_
		
		Stats_.Parent = JoiningPlayer
		
		while true do
			wait(1)
			Seconds.Value = Seconds.Value + 1
		end
	end
)

However, I might want to use this module in my client-sided code. For example, if I have a GUI that displays a list of all the players in the game, I want it to list players who are already in-game and players who join later so that no players get missed when constructing the list. The module from above would come in handy here.

The first issue is that client-sided code cannot require on-site modules. The following code is supposed to just print the name of someone when they join or, if they’re already in-game, when the code runs.

Code:

require(5871065556)(
	function(JoiningPlayer)
		print(JoiningPlayer.Name)
	end
)

Output:

> 18:00:29.162 - require(assetId) cannot be called from a client. assetId = 5871065556

This makes sense. Obviously the client should not be able to pull code from anywhere, among other assets often uploaded alongside on-site ModuleScripts.

There is a solution to this: have the server re-parent (and rename) the ModuleScript from within its code so that the client can access it from within the DataModel.

Inside the now-updated MainModule from the top of this post is the following:

script.Name = "Apply Function To All Players"
script.Parent = game:GetService("ReplicatedStorage")

I changed the client-sided code’s require function to look like this:

local Module = game.ReplicatedStorage["Apply Function To All Players"]
require(Module)(
	function(JoiningPlayer)
		print(JoiningPlayer.Name)
	end
)

while the server-sided require code did not change.

After the game starts, the DataModel looks like this:

but then the clientside output shows a new error:

> 18:17:33.041 - Module code did not return exactly one value

Opening up the ModuleScript on the server, everything appears fine:

image

but when I switch to the client and open the script, it shows a different story:

image

The ModuleScript is completely empty.

It seems like a bug that ModuleScript code is not replicating to clients, regardless of how it ended up in the game. It should be noted, too, that any descendant modules of the MainModule in question work fine on the client when re-parented.

Testing place for the bug: OnSiteModuleError.rbxl (21.5 KB)

1 Like