Datastore saving into other players

I want to create a datastore that saves, trails, auras, coins, and wins, but my datastore has issues of sometimes not saving, and saving into other players.

local DataStoreService = game:GetService("DataStoreService")
local DataStore1 = DataStoreService:GetDataStore("DataStore1")

game.Players.PlayerAdded:Connect(function(plr)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr

	local Coins = Instance.new("IntValue")
	Coins.Name = "Coins"
	Coins.Parent = leaderstats

	local Wins = Instance.new("IntValue")
	Wins.Name = "Wins"
	Wins.Parent = leaderstats

	PlrData = Instance.new("Folder")
	PlrData.Name = "PlrData"..plr.UserId
	PlrData.Parent = game.ReplicatedStorage
	
	plr.CharacterAdded:Connect(function(char)
		
		plr.Backpack:ClearAllChildren()
		plr.StarterGear:ClearAllChildren()
		PlrData:ClearAllChildren()
		
		game.ReplicatedStorage.PlrData:Fire(PlrData)
		
		local data1
		local data
		local coinsStored
		local winsStored
		local success, errormessage = pcall(function()
			data = DataStore1:GetAsync(plr.UserId.."tools")
			data1 = DataStore1:GetAsync(plr.UserId.."trails")
			coinsStored = DataStore1:GetAsync(plr.UserId.."coins")
			winsStored = DataStore1:GetAsync(plr.UserId.."wins")
		end)

		if coinsStored ~= nil then
			Coins.Value = coinsStored
		end

		if winsStored ~= nil then
			Wins.Value = winsStored
		end

		if data ~= nil then
			for _, toolName in pairs(data) do
				local tool = game.ServerStorage.ShopItems:FindFirstChild(toolName):WaitForChild(toolName) 
				if tool:IsA("Tool") then
					local NewTool = tool:Clone()
					NewTool.Parent = plr.Backpack
					local NewTool = tool:Clone()
					NewTool.Parent = plr.StarterGear
				end
			end
		end	
		
		if data1 ~= nil then
			for _, trailName in pairs(data1) do
				local trail = game.ServerStorage.ShopItems:FindFirstChild(trailName):FindFirstChild(trailName)
				if trail:IsA("Trail") or trail:IsA("BasePart") then
					local NewTrail = trail:Clone()
					NewTrail.Parent = plr.Character.Head
					local NewTrail = trail:Clone()
					NewTrail.Parent = PlrData
				end
			end
		end
	end)
end)

game.Players.PlayerRemoving:Connect(function(plr)
	plr.CharacterRemoving:Connect(function(char)
		local toolTables = {}

		for _, tool in pairs(plr.StarterGear:GetChildren()) do
			if game.ServerStorage.ShopItems:FindFirstChild(tool.Name) then
				table.insert(toolTables, tool.Name)
			end
		end

		local trailTables = {}

		for _, tool in pairs(PlrData:GetChildren()) do
			if game.ServerStorage.ShopItems:FindFirstChild(tool.Name) then
				table.insert(trailTables, tool.Name)
			end
		end


		local success, errormessage = pcall(function()
			DataStore1:SetAsync(plr.UserId.."tools",toolTables)
			DataStore1:SetAsync(plr.UserId.."coins",plr.leaderstats.Coins.Value)
			DataStore1:SetAsync(plr.UserId.."wins",plr.leaderstats.Wins.Value)
			DataStore1:SetAsync(plr.UserId.."trails",trailTables)
		end)

		if success then
			print("saved")
		else
			warn(errormessage)
		end
	end)
end)

game:BindToClose(function()
	for _, plr in pairs (game.Players:GetPlayers()) do

		local toolTables = {}

		for _, tool in pairs(plr.StarterGear:GetChildren()) do
			if game.ServerStorage.ShopItems:FindFirstChild(tool.Name) then
				table.insert(toolTables, tool.Name)
			end
		end
		
		local trailTables = {}
		
		for _, tool in pairs(PlrData:GetChildren()) do
			if game.ServerStorage.ShopItems:FindFirstChild(tool.Name) then
				table.insert(trailTables, tool.Name)
			end
		end

		local success, errormessage = pcall(function()
			DataStore1:SetAsync(plr.UserId.."tools",toolTables)
			DataStore1:SetAsync(plr.UserId.."coins",plr.leaderstats.Coins.Value)
			DataStore1:SetAsync(plr.UserId.."wins",plr.leaderstats.Wins.Value)
			DataStore1:SetAsync(plr.UserId.."trails",trailTables)
		end)

		if success then
			print("saved")
		else
			warn(errormessage)
		end
	end
end)
2 Likes

PlrData is a global variable so it will change if it’s changed from the original scope or another scope. I would recommend not using globals. Instead, use tables to sort different players’ data.

2 Likes

Try use

local PlrData = Instance.new("Folder")

It can’t be used in another scope

Make a local function for the CharacterAdded event, that way it triggers every time a player respawns, you can also use game.Players:GetPlayerFromCharacter(Chr) to get the player.