Storing data in _G

Hi.
I recently created a game in which I kept player data in module.data = {}. In a single test everything worked fine, but when I released the game - here appeared categorically important bugs. The data literally synchronized and mixed up. although it had the structure {id = {}, id2 = {}}. I read that keeping data in modules is not the best way. Advise where it is possible to keep player data (in addition to _G if it is suitable for this.)
The game is not complicated, the average number of calls to data if you need to know. maybe 20-
40 times per second

code:

module:

local module = {}
module.data = {}
return module

call:

require(script.Parent.ModuleScript).data[id]

1 Like

A module? Why do you use _G if you can use a simple module or a script to manage the data?

If you haven’t read what I wrote, I’ll repeat myself. The data in my module is simply mixed or synchronized with the data of other players.

It’s unclear to me what you’re trying to do. If you want to store data, you should have a look at datastores Data Stores | Documentation - Roblox Creator Hub.

If you just want to track data in the current runtime you can just use a module. You can write a singleton, which will return the same data wherever you require it from. Here’s an old module I made, which hides the data so you can’t misuse it

--!strict
local PlayerData = {}

-- Internal data storage
local data = {}

-- Sets a key-value pair for a specific player
function PlayerData:Set(player : Player, key : any, value)
	if not data[player] then
		data[player] = {}
	end
	data[player][key] = value
end

-- Retrieves the value associated with a key for a specific player
function PlayerData:Get(player : Player, key : any?)
	--assert(key ~= nil, "Key cannot be nil in PlayerData:Get")
	
	return data[player] and key and data[player][key] or data[player] or nil
end

-- Retrieves all data for all players
function PlayerData:GetAll()
	return data
end

-- Checks if a specific key exists for a player
function PlayerData:HasKey(player : Player, key : any)
	return data[player] and data[player][key] ~= nil
end

-- Updates a key's value for a player if the key exists
function PlayerData:Update(player : Player, key : any, value : any)
	if data[player] and data[player][key] ~= nil then
		data[player][key] = value
	end
end

-- Removes all stored data for a specific player
function PlayerData:Remove(player : Player)
	data[player] = nil
end

-- Clears all stored data
function PlayerData:Clear()
	data = {} -- Resetting the table instead of clearing each key
end

return PlayerData

That is an issue with your implementation and not an inherent issue with modules. If your state is being corrupted then you need to check your data structures or how you access your data structures. There is probably a bug in the logic of your code that is mixing up references to different players’ data.

Where did you read this? I’ve never heard anything of the sort. _G is a global table that functions just like a table returned by a module, so using it won’t make a practical difference. Whatever you end up doing will be using tables anyway; I can’t imagine any alternatives.

Not sure what you’re talking about here. I hope this isn’t saving data this many times …
20-40 times a second is way too much.

Sounds like you came up with a workaround to using a datastore. When using a datastore is in fact very simple and should fix any errors you’re running into now.

Best answer I can give with the information given.