Data store loading onto incorrect player

I have this leaderstats/datastore script in my game, and on multiple occasions it has set my stats onto another player, which then saves when they leave. I’ve had to reset multiple players’ stats because of this. Is there a problem with the script that could be fixed? I’m not really sure how to go about it, since it saves data to a player’s user id I don’t know how it could mix them up.

local datastoreservice = game:GetService("DataStoreService")
local datastore = datastoreservice:GetDataStore("datastore")
local coindatastore = datastoreservice:GetDataStore("coindatastore")
local particledatastore = datastoreservice:GetDataStore("particledatastore")
local winsleaderboard = datastoreservice:GetOrderedDataStore("datastore")

game.Players.PlayerAdded:Connect(function(plr)
	local stats = Instance.new("Folder", plr)
	stats.Name = "leaderstats"
	local wins = Instance.new("IntValue", stats)
	wins.Name = "Wins"
	--coins
	local coins = Instance.new("IntValue", plr)
	coins.Name = "Coins"
	
	local id = "Player_"..plr.UserId
	
	local success, errormessage = pcall(function()
		data = datastore:GetAsync(id)
		--print(datastore:GetAsync(id))
		cdata = coindatastore:GetAsync(id)
		--print(coindatastore:GetAsync(id))
		pdata = particledatastore:GetAsync(id)
		--print(pdata)
	end)
	
	if success --[[and id == "Player_"..plr.UserId]] then
		
		if plr.UserId == "128940106" then
			wins.Value = 200
			coins.Value = 10000
		else
			wins.Value = data
			coins.Value = cdata
		end
		
		if pdata ~= nil then
			for i, v in pairs(pdata) do
				if game.ReplicatedStorage:FindFirstChild(v) then
					local newparticle = game.ReplicatedStorage:FindFirstChild(v):Clone()
					newparticle.Parent = plr:WaitForChild("Inventory")
					--print(v.." should appear in your inventory")
				elseif game.ReplicatedStorage.ParticleTextures:FindFirstChild(v) then
					local newparticle = game.ReplicatedStorage.ParticleTextures:FindFirstChild(v):Clone()
					newparticle.Parent = plr:WaitForChild("Inventory")
					--print(v.." should appear in your inventory")
				end
			end
		end
	end
	game.ReplicatedStorage.InventoryLoaded:FireClient(plr)
	plr.PlayerGui:WaitForChild("ScreenGui").Coins.TemporaryCoinValue.Text = plr.Coins.Value

	plr.CharacterAdded:Connect(function(character)
		
		character.Humanoid.Died:Connect(function()
			
			if character:FindFirstChild("GameTag") then
				character.GameTag:Destroy()
				plr:LoadCharacter()
				
			end
		end)
		
	end)
	
	if plr.MembershipType == Enum.MembershipType.Premium --[[and plr.Inventory:FindFirstChild("Premium") == nil]] then
		local premium = game.ReplicatedStorage.ParticleTextures.Premium:Clone()
		premium.Parent = plr.Inventory
	end
	local marketplace = game:GetService("MarketplaceService")
	if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(plr.UserId, 10339057) and plr.Inventory:FindFirstChild("VIP") == nil then
		local vip = game.ReplicatedStorage.ParticleTextures.VIP:Clone()
		vip.Parent = plr.Inventory
	end
	
	if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(plr.UserId, 10461046) and plr.Inventory:FindFirstChild("USA Fireworks") == nil then
		local fireworks = game.ReplicatedStorage.ParticleTextures:FindFirstChild("USA Fireworks"):Clone()
		fireworks.Parent = plr.Inventory
	end
		
	--[[
	if plr.Inventory:FindFirstChild("Beta") == nil then
		local Beta = game.ReplicatedStorage.ParticleTextures.Beta:Clone()
		Beta.Parent = plr.Inventory
	end
	]]
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local id = "Player_"..plr.UserId
	local windata = plr.leaderstats.Wins.Value
	local coindata = plr.Coins.Value
	--Sets wins and coin data
	datastore:SetAsync(id, windata)
	coindatastore:SetAsync(id, coindata)
	--Sets particle data
	local equippedparticle = plr.EquippedItem:GetChildren()
	equippedparticle[1].Parent = plr.Inventory
	local particles = {}
	for i, v in pairs(plr.Inventory:GetChildren()) do
		if game.ReplicatedStorage.ParticleTextures:FindFirstChild(v.Name) then
			table.insert(particles, v.Name)
		end
	end
	
	local success, errorMessage = pcall(function()
		particledatastore:SetAsync(id, particles)
	end)
	
end)



game:BindToClose(function()
	for i, plr in pairs(game.Players:GetPlayers()) do
		local id = "Player_"..plr.UserId
		local windata = plr.leaderstats.Wins.Value
		local coindata = plr.Coins.Value
		--Sets wins and coin data
		datastore:SetAsync(id, windata)
		coindatastore:SetAsync(id, coindata)
		--Sets particle data
		
		local particles = {}
		for i, v in pairs(plr.Inventory:GetChildren()) do
			if game.ReplicatedStorage.ParticleTextures:FindFirstChild(v.Name) then
				table.insert(particles, v.Name)
			end
		end
		
		local success, errorMessage = pcall(function()
			particledatastore:SetAsync(id, particles)
		end)
	end
end)

Edit: To clarify, it loads the “datastore” and “coindatastore” but not “particledatastore”, which I think is pretty odd. Any help is appreciated

might I ask, what is this for?

My friend joined, loaded my stats, and got 800 free wins. I tried to reset them haha

1 Like

You can just RemoveAsync their saved data. You shouldn’t hardcode a check like this, but it wouldn’t work anyways. Their UserId will always be a number, and never equal to the string “128940106”.

2 Likes

So if I were to reset or edit their stats through their user id I would just type the number and not turn it into a string?

Yeah, but you should RemoveAsync it in the command bar in Studio (or the F9 menu in-game) rather than hard-coding a check to reset it.

3 Likes