Hello, i’m trying to update this variable called PlotPositions in this module script, but when i update it, using the update function, the second function trying to acces it prints nil.
Any help is appreciated
P.S this module script is located in Replicated storage, and two other scripts calling it, one from serverscriptservice who calls the update function and the second one is a local script calling it every second
Script:
local module = {}
local plotPositions
function module:UpdatePlotPositions(plotPosTable)
plotPositions = plotPosTable
print(plotPositions) -- does not print nil
end
function module:LoadChunk(chunkPosX, chunkPosZ)
print(plotPositions) -- prints nil
end
return module
-- after a certain amount of time another script is done doing stuff and it fires this function
local Script = require(ReplicatedStorage.ModuleScript)
Script:UpdatePlotPositions(Table)
-- I'm making a chunk loading system, so LoadChunk is called pretty much every second
local Script = require(ReplicatedStorage.ModuleScript)
while true do
task.wait(1)
Script:LoadChunk()
end
did u make a new require again in the script or is it in diff script, if u make new req it will get the original module, for example :
Module :
local module = {}
module.storeVar = function(name, value)
module[name] = value
end
return module
some script : — This is the correct way
local req1 = require(thatonemodule)
req1.storeVar("fax", "some") -- store the "fax" and "some" to the module
print(req1["fax"]) -- it will print "some" since the function add the name and value to the module
some script again :
local req1 = require(themodule) -- require the module
req1.storeVar("wow", "haii") -- store wow for haii to the module
print(req1["wow"]) -- it will print haii
local req2 = require(thomodule) -- require a NEW module
print(req1["wow"]) -- it will print haii
print(req2["wow"]) -- it will print nil since its different require
Thats not true, that will print “haii”, you can only require() once per script, and once its done its allocated in memory. Even if you do a new require and store it in a new variable, both are pointing to the same module.
I tested your example:
local req1 = require(script.Parent:WaitForChild("ModuleScript"))
req1.storeVar("wow", "haii")
print(req1["wow"])
local req2 = require(script.Parent:WaitForChild("ModuleScript"))
print("from req1:", req1["wow"])
warn("from req2:", req2["wow"])
It seems that you’re experiencing an issue where the plotPositions variable is not retaining its updated value when accessed in the LoadChunk function. This behavior could occur due to the scope of the plotPositions variable.
To ensure that the updated value is accessible in other functions, you can modify your module script as follows:
local module = {}
module.plotPositions = nil
function module:UpdatePlotPositions(plotPosTable)
module.plotPositions = plotPosTable
print(module.plotPositions) -- prints the updated value
end
function module:LoadChunk(chunkPosX, chunkPosZ)
print(module.plotPositions) -- prints the updated value
end
return module
In this modification, plotPositions is defined as a field of the module table using module.plotPositions. By doing this, it becomes accessible to other functions within the module. Now, when you update the plotPositions variable using the UpdatePlotPositions function, the updated value will be stored in module.plotPositions and can be accessed by other functions like LoadChunk.
Make sure that you update the code in both the server script and the local script to reflect these changes.
I hope this resolves the issue you were facing. If you have any further questions, feel free to ask!
This did get me a bit further, however it didn’t work.
I noticed that if a make a new script (server script), and print Module.plotPositions,
it actually prints the table, but if I try to print it the same way, but using a local script, it just prints nil,
meaning the local script does not detect the changes made by the serverscript, even after calling the module
Edit: i got it working using a remote event, with the plotPositions value passed trough it, and then updating that value on the local side, instead of updating it on the serverside, if you have any other solutions i will try them out