Saving Session Data in a Module

Hey everyone, I have never posted on this site before but I can’t find the solution to a problem I’ve been having. Previously I have been handling session data with a physical structure (folders and values) but I want to move on to using modules instead. This has nothing to do with data stores, its purely the module itself that’s giving me trouble. The code itself works fine - I based it off of the example they had on the wiki here: Documentation - Roblox Creator Hub. The issue is that whenever I save new data to the module, it somehow replicates that to every individual table for each player in the game. So if I set playerData[player.Name][key] = 1, then every player.Name table will have “key” set to 1. I’ve tried messing around with print lines to see if its setting multiple times but it appears to only be doing it once. If anyone has any ideas of what I can try that would be much appreciated. I’ll post the code below as well if that helps. Thanks!

--Services
local repStorage = game:GetService("ReplicatedStorage")

--Data Components
local events = repStorage:WaitForChild("Events")

--Modules
local mod_DefaultPlayer = require(script.DefaultPlayer)
local mod_Keybinds = require(script.Keybinds)

--Events
local rem_Data = events:WaitForChild("Data")

--Variables
local module, playerData = {}, {}

--Functions
function printAll() --For Debugging
	print("ALL DATA:")
	for i, v in pairs(playerData) do
		print(i)
		for e, x in pairs(v) do
			if type(x) == "table" then
				print("   [table]")
			else
				print("   "..e.." = "..x)
			end
		end
	end
end


--API
function module:Create(player)
	--print("Creating data for "..player.Name)
	if not playerData[player.Name] then
		playerData[player.Name] = mod_DefaultPlayer.new() --Another workaround I tried
		playerData[player.Name]["Keybinds"] = mod_Keybinds
		--Add inventories and such
		
		rem_Data:FireClient(player)
	end
end

function module:Remove(player)
	if playerData[player.Name] then
		playerData[player.Name] = nil
	end
end

function module:Get(player,key)
	--print("Requesting: "..player.Name.. " "..key)
	--printAll()
	if key then
		return playerData[player.Name][key]
	else
		return playerData[player.Name]
	end
end

function module:Set(player,key,value)
	print("Requesting: "..player.Name.. " "..key.. " = "..value)
	playerData[player.Name][key] = value --HERE: This seems to set every player's key to value
	rem_Data:FireClient(player) --Just informing the client
end


return module

Could you post the code for mod_DefaultPlayer.new? You might be assigning the same table to every player instead of a unique table for each person.

I definitely was before so I tried this but it didn’t seem to help :confused:

local module = {}
local data = {

	Class = "Warrior",
	Level = 1,
	Primary = "Bone",
	Secondary = "Makeshift Shield"

}


--API
function module.new()
	local newData = {}
	for i,v in pairs(data) do
		newData[i] = v
	end
	return newData
end

return module
1 Like

I can say that you need to use player.UserId for the key instead of player.Name, since the username can change with the user. This wouldn’t fix your problem though, it would just guarantee that duplicate names isn’t the problem. You could just use Find and Replace and replace every instance of player.Name with player.UserId.

Also, how do you know that that this sets every players’ data? Did you just test it in a local server?

1 Like

I’ve tested it with both a local server and in a published FE instance with 5 or 6 people. Both seemed to save the same issues. It was almost as if every player was sharing one set of data. Good idea with the UserId though I will definitely add that in.

2 Likes

Could you include any mention of code that uses module:Set in your scripts? I’m asking because I can’t seem to find any problems with your module.

The only reason I can think of for why this is happening is because you are assigning the same table to the key in your module using module:Set, instead of creating a unique table.

1 Like

You were right! The issue wasn’t the module it was the :Set calls. I was storing a bunch of modules in Replicated Storage and those were then calling the :Set which explains why that call was being replicated for each player. Still seems a little strange that it would even work that way but when I keep those calls on the Server there doesn’t seem to be an issue anymore. Thanks for the help @goldenstein64 !

3 Likes