Data being changed for players that it shouldn't?

Alright so I have this datastore setup for my game and loading/saving works great and for the sake of keeping this post a simple as posible ive removed the saving part of it

So when a player joins I load their data if they have some and I create some for them if they dont, I also have this sort of data manager cache thing that creates a new object and stores that data I either loaded or created, and that works great

So this is that whole part

pcall(function()
	local data = PlayerDataStore:GetAsync(tostring(player.UserId))
		
	if not data then
		local cache = Manager.new(player, Template)
		PlayerDataStore:SetAsync(tostring(player.UserId), Template)
		
		print("Created new data for "..player.Name)
	else
		local reconciledData = Reconcile(data)
		PlayerDataStore:SetAsync(tostring(player.UserId), reconciledData)
		local cache = Manager.new(player, reconciledData)
		
		print("Loaded data for "..player.Name)
	end
		
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
		
	local stage = Instance.new("IntValue")
	stage.Name = "Stage"
	stage.Value = Manager[player]:Get("Stage")
	stage.Parent = leaderstats
end)

And then for the Manager module this is what that looks like

local Manager = {}

function Manager.new(player : Player, data)
	local self = setmetatable({}, {__index = Manager})
	
	self.player = player
	self.data = data
	
	Manager[self.player] = self
	
	return self
end

function Manager:IncrementStage()
	local leaderstats = self.player:WaitForChild("leaderstats")
	local stage : IntValue = leaderstats.Stage
	stage.Value += 1
	self:Set("Stage", stage.Value)
end

function Manager:Get(key)
	return self.data[key]
end

function Manager:Set(key, newValue)
	self.data[key] = newValue
end

return Manager

So the main problem is that when I’m testing with another player (on my phone) and testing on my computer with both players with no data I have player1 go to stage 2 and I leave player2 where they are and I have player2 leave the game and when a player leaves I’ve been printing their Manager object and this is the code for that

game.Players.PlayerRemoving:Connect(function(player)
	for key, tbl in pairs(Manager[player]) do
		if key == "data" then
			for j,k in pairs(tbl) do
				print(j ,k)
			end
		end
	end
end)

And for some reason It’s incrementing the stage for the player that didnt even go to stage 2 and Ive been trying to figure out why and I just dont know whats going on, any ideas?

Alright so I fixed it so basically when a player who never joined the game would join, I was setting the cache not to the new set data and I dont know for some reason it messed everything up but now its fixed, so setting the cache to the newly set data, so I set then I get and then use what i got and set the cache to it!

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