Global Table In Two Scripts

Can two server scripts change the content of a common table stored in a module script?
I’m asking this because i Have a remote Function that Changes the content of the Global Table that contains all the players. When the Remote Function Is Fired the server goes to the Global table and changes the content according to the id of the player, but for some reason the content is not changed correctly.

–ModuleScript–

local module = {}
module.__index = module

local Global = {}

function module:InsertPlayer(id)
	
	if Global[id] == nil then
		
		Global[id] = {}
		table.insert(Global[id], {["Alive"] = true, ["Anims"] = false })
		
		
	else
		
		error("there already exists a player named :"..id	)
		
	end
	
end

function module:ChangeState(id : number, state : string, value: boolean) -- this function only allows one value to be true
	
	if Global[id] ~= nil then

		
		for i, v in pairs(Global[id][1]) do
			
			
			if i == "Q" then
				
			else
				
				Global[id][1][i] = false
			--	print(Global[id][1])
				
			end
			
			
		end
		
		Global[id][1][state] = value
		--print(Global[id][1])
	else

		error(id	.." does not exist")

	end
	
end

Unless the table is exposed by returning it within the module table itself, no one can modify it. Unless you make a function that’s exposed to other scripts allowing them to set values to it. You can have control over that though so I don’t see a problem with that.

The server can have a copy of the Table trough module:GetPlayerData() and can change trough module:ChangeState(). but for some reason when I test the game with two players the content of the tables seems to not agree with each other making somethings what is supposed to happen on player A, happen on player B

Is this module script run locally?

no, its all on the server.
Does the code inside a Remote Function run kind of locally?
I change the values of the table inside a Remote function.
The player parsed in the function is then used to access the correct part of the table.
but for some reason when I try to read other player’s table content it seems to be wrong and sometimes equal to my player’s table contents.

Can you explain me what exactly your system does in detail?

Yes
In a local script a Remote Function Can be Called
The server Picks up the request and changes the content of a global table stored in a module script.
For some reason when , for example Player A fires the event and tries to read the other players values
the values are wrong.

and the tables seem to mix up with one another

Perhaps you should look into SharedTables, instead.

If the data is being read on the server, then it should have no problems reading it.
Are you sure it’s on the same context level? You’re not trying to read the module from the client after setting it on the server?

1 Like

yes it all on the server and in the module scrip

the server makes “a request” to the Module script and the module scripts changes the values

maybe i just have to take a better look at my code and make some prints. That you for your time

1 Like

After checking my code. I saw the error. The scripts and module are storing and saving the values correctly. Thank you @TheRealANDRO for helping me

1 Like

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