Variable is not getting updated

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

is the second function called in the same script as where first function is called?

yes, both functions are in the same script

How are you calling the methods?

the update function is called once, after around 10 seconds after the game has started

and the LoadChunk function is being called multiple times a second and needs to wait until the update function is called

I mean like a visual of your code

1 Like

Well it looks something like this:

-- 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
1 Like

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
1 Like

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"])

Heres the output:
image

2 Likes

Try OOP it’s really usefull. All about Object Oriented Programming

Cool… but… What I should do with OOP? if I dont have an issue with something? :v
What exactly you mean by sending me to study OOP? xD

Wrong reply, I meant the guy. Who try to update his plots. because I did something simular with appartements.

1 Like

Hi yes sorry for late reply

I will look into the things you said later

Hello schaap5347,

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!

1 Like

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

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