Data is being shared with everyone

MY ISSUE: Everyone data is the same after random generating a value.
→ random generator works FINE
→ data loads but everyone has the SAME data after being random generated

Here’s the code:

function DataHandler:RandomItemFromTbl(tbl)
	local RNG = Random.new();
	local counter = 0;
	for i,v in pairs(tbl) do
		counter += tbl[i][2]
	end
	local chosen = RNG:NextNumber(0, counter)
	for i,v in pairs(tbl) do
		counter -= tbl[i][2]
		if chosen > counter then
			return tbl[i][1]
		end
	end
end

function DataHandler:characterSetUp(player)
	if DataManager.Profiles[player].Data.IsFirstLoad ~= true then
		DataManager.Profiles[player].Data.IsFirstLoad = true
		if DataManager.Profiles[player].Data.Race == "None" then
			local newRace = DataHandler:RandomItemFromTbl(Info.Races)
			DataManager.Profiles[player].Data.Race = newRace
		end
	end


end

local function playerAdded(player)
	local profileData = ProfileStore:LoadProfileAsync("Player_"..player.UserId)

	if profileData ~= nil then
		profileData:AddUserId(player.UserId)
		profileData:Reconcile()

		profileData:ListenToRelease(function()	
			DataManager.Profiles[player] = nil
			player:Kick("Data error at releasing")
		end)

		if player:IsDescendantOf(game.Players) then
			DataManager.Profiles[player] = profileData
			DataHandler:characterSetUp(player)
			print(DataManager.Profiles[player].Data)
		else
			profileData:Release()
		end

	else
		player:Kick("Data error")
	end

end



function DataHandler:Init()
	for _, player in pairs(game.Players:GetPlayers()) do
		task.spawn(playerAdded,player)
	end
	game.Players.PlayerAdded:Connect(playerAdded)

	game.Players.PlayerRemoving:Connect(function(player)
		if DataManager.Profiles[player] ~= nil then
			DataManager.Profiles[player]:Release()
		end
	end)
end

Anything will help :pray:

I’m not entirely sure what is happening here, but the issue is probably in the tables and how they work.

For example, if you do this:

--Pseudocode
local startingData = {
    Races = {}
}

game.Players.PlayerAdded:Connect(function(player)
    if isNewPlayer(player) then
        DataManager.Profiles[player].Data = startingData
    end
end)

changes with the players’ data will be shared between all players, because you just assign links to the “startingData” table, instead of making new tables for each player

Instead, you should do something like this:

--Pseudocode
local startingData = {
    Races = {}
}

game.Players.PlayerAdded:Connect(function(player)
	if isNewPlayer(player) then

		local newTable = {} -- Creating new table for this player
		for key, value in pairs(startingData) do -- Cloning "startingData" table into "newTable"
			if type(value) == "table" then
				newTable[key] = {} -- Creating new "Races" table for "newTable"
			else
				newTable[key] = value
			end
		end

		DataManager.Profiles[player].Data = newTable
	end
end)

I hope this will help

1 Like

Hey, my bad I removed the part DataManager.Profiles[player].Data = DefaultStats because it was meant for some testing(edited the first post). Anyways my table is also like this

local DefaultStats = {
	Race = "None",
        IsFirstLoad = false,
}

I will test what u told me in a bit :3

Yeah this doesnt work and it’s not something I want to add to the code