Module Script variable won't change

I want the script to set the variable “Requester” in the ModuleScript called “Data” to player who activated the RemoteEvent from LocalScript.

For some apparent reason, it won’t allow me to do that and when I try to call the module script variable it returns the default variable (nil)

I tried many things but they didn’t work, what am I doing wrong?

Context:

  • MainScript is in the tool
  • Handler is in the ServerScriptService
  • Data is in the GUI
  • LocalScript is in GUI.Frame.Accept (TextButton)
  • GUI is in the ServerStorage

MainScript:

local tool = script.Parent
local remote = tool.Select
local Players = game:GetService("Players")
local QuestionGui = game.ServerStorage.CarryQuestion
local victim

remote.OnServerEvent:Connect(function(plr, target)

	victim = Players:GetPlayerFromCharacter(target)

	if victim then
		if not victim.PlayerGui:FindFirstChild("CarryQuestion") then
			local pu = QuestionGui:Clone()
			
			local module = require(pu:WaitForChild("Data"))
			module.Requester = plr
			
			local userId = plr.UserId
			local thumbType = Enum.ThumbnailType.HeadShot
			local thumbSize = Enum.ThumbnailSize.Size420x420
			local content, isReady = Players:GetUserThumbnailAsync(userId, thumbType, thumbSize)


			local imageLabel = pu.Frame.RequesterImage
			imageLabel.Image = content
			
			pu.Parent = victim.PlayerGui
		end
	end

end)

Handler:

-- The script isn't finished as I was testing ways to get the ModuleScript working

local remote = game.ReplicatedStorage.CarrySelect
local Players = game:GetService("Players")
local guiDecline = game.ServerStorage.CarryDecline
local victim

remote.OnServerEvent:Connect(function(victim, requester, isAccept, bool, gui)
	
	print(requester)
	print(victim)
	
	gui.Frame:TweenPosition(UDim2.new(0, 0, 1, 0), "InOut","Sine",0.5)
	
	if isAccept == true then

		if bool == true then
			print("true :D")
		end

	elseif isAccept == false then

		local uhoh = guiDecline:Clone()
		uhoh.Frame.Text.Text = requester.." declined your carry request."
		uhoh.Parent = requester.PlayerGui

	end

end)

Data:

local module = {}

module.Requester = nil

return module

LocalScript:

local ACbutton = script.Parent
local remote = game.ReplicatedStorage.CarrySelect
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local Players = game:GetService("Players")
local gui = script.Parent.Parent.Parent

local module = require(gui.Data)

ACbutton.MouseButton1Click:Connect(function()

	local requester = module.Requester

	if not plr.Character:HasTag("IsCarry") and not plr.Character:HasTag("DoesCarry") then
		remote:FireServer(requester, true, true, gui)
	elseif not plr.Character:HasTag("IsCarry") and plr.Character:HasTag("DoesCarry") then
		remote:FireServer(requester, true, false, gui)
	end

end)
1 Like

I didn’t read the whole long post but I am assuming you are assuming ModuleScripts have shared tables across server client.

ModuleScripts do not have shared tables across server client. They run independently.

So a ServerScript cannot change the variables in the ModuleScript?

Each computer can change variables for themselves and not other computers

To send information from one computer to the other you must use RemoteEvents or RemoteFunctions

(Or instances that automatically replicate like instances in replicated storage and workspace)

So to change a variable in the ModuleScript (e.g. module.Number from 0 to 1) I, instead of typing in a ServerScript

module.Number = 1

I need to use a RemoteEvent?

Each computer can change variables for themselves and not other computers

To send information from one computer to the other you must use RemoteEvents or RemoteFunctions

Bro I’m talking about Server Script and Module Script communication, not computers or Client-Server communication.

That’s why I said I didn’t read the whole thing (I don’t like reading long things) and assumed you were assuming the ModuleScript had a shared table across server client. Since it is a very common mistake people make.

I’ll go back up to the original post and read the whole long thing and see if I spot any mistakes and get back to you.

Okay I am back. It looks like you were making the common mistake I was referring to.

Let me break it down.

This is from MainScript which is a Script in tool (means it runs on the server)

Then in a LocalScript (means it runs on the client) you try to access module.Requester


Since ModuleScripts don’t share tables across computers. When you set the variable on the server it will not change anything on the client.

Let’s do an easy example with a few lines.

-- ModuleScript in ReplicatedStorage
local module = {}

module.Requester = nil

return  module
-- Script in ServerScriptService
local module = require(game.ReplicatedStorage.ModuleScript)

module.Requester = "Testing123"

print(module.Requester) --> "Testing123"

-- printing module.Requester from any server script after this runs will print "Testing123"
-- LocalScript in StarterPlayerScripts
local module = require(game.ReplicatedStorage.ModuleScript)

while wait(1) do
   print(module.Requester) -- This will print 'nil' forever
end

As you can see since we changed “module.Requester” on the server, any script that is on the same computer (the server) will be able to see that change. But since ModuleScripts don’t share tables across computers, the client will not be able to see any changes to the ModuleScript table that happened from the server. It is like they are running two separate scripts.

The only way to get data from one computer to another (server to client or vice versa) is with RemoteEvents and RemoteFunctions.

Simply saying; LocalScripts do not see changes in ModuleScript variable changes.

Not quite.


You can phrase it like this:

And:

So putting it simply in one sentence:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.