Tool/backpack Shop Buy saving script is not saving data

local dataStoreService = game:GetService(“DataStoreService”)
local dataStore = dataStoreService:GetDataStore(“BackpackSave”)

game.Players.PlayerAdded:Connect(function(player)
pcall(function()
local tool = dataStore:GetAsync(“User-”…player.UserId)
if tool then
for i,v in pairs(tool) do
local toolFound = game.ReplicatedStorage.Items:FindFirstChild(v)
if toolFound then
toolFound:Clone().Parent = player.Backpack
toolFound:Clone().Parent = player.StarterGear
end
end
end
end)
end)

game.Players.PlayerRemoving:Connect(function(player)
pcall(function()
local toolSave = {}
for i, tool in pairs(player.Backpack:GetChildren()) do
if tool then
table.insert(toolSave,tool.Name)
end
end
dataStore:SetAsync(“User-”…player.UserId, toolsSave)
end)
that is my script i have a folder in replicated storage and it is named Tools inside of it the names are item1, item2, item3 and so on
My script wont work whenever i purchase in the shop i get a tool then after that the tool wont save it just has me buy it again if you could tell me what i did wrong that will be awesome and thank you!

1 Like
local dataStoreService = game:GetService(“DataStoreService”)
local dataStore = dataStoreService:GetDataStore(“BackpackSave”)

game.Players.PlayerAdded:Connect(function(player)
pcall(function()
local tool = dataStore:GetAsync(“User-”…player.UserId)
if tool then
for i,v in pairs(tool) do
local toolFound = game.ReplicatedStorage.Items:FindFirstChild(v)
if toolFound then
toolFound:Clone().Parent = player.Backpack
toolFound:Clone().Parent = player.StarterGear
end
end
end
end)
end)

game.Players.PlayerRemoving:Connect(function(player)
pcall(function()
local toolSave = {}
for i, tool in pairs(player.Backpack:GetChildren()) do
if tool then
table.insert(toolSave,tool.Name)
end
end
dataStore:SetAsync(“User-”…player.UserId, toolsSave)
end)

Nothing seems to be wrong but put your folder in The Server Storage to prevent hackers steal the items from that folder

Make sure that api services is enabled

I have cleaned up your code and fixed some issues and implementation, this should work. Please not that data saving inside of roblox studio is a bit iffy if you don’t use a BindToClose method for some reason. I suggest if you don’t use a BindToClose method, then test inside of an actual roblox server which will be outside of studio.

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local DataStore = DataStoreService:GetDataStore("Storage")
local ItemsFolder = ReplicatedStorage:FindFirstChild("Items")

Players.PlayerAdded:Connect(function(Player)
	
	local loadedData
	local worked , err = pcall(function()
		loadedData = DataStore:GetAsync(Player.UserId)
	end)
	
	if loadedData ~= nil and worked then
		for i , v in pairs(loadedData) do
			local Tool = ItemsFolder:FindFirstChild(v)
			if Tool then
				Tool:Clone().Parent = Player.Backpack
				Tool:Clone().Parent = Player.StarterGear
			end
		end
	end
	
	if not worked then warn(err) end
	
end)

Players.PlayerRemoving:Connect(function(Player)
	local tools = {} -- table for saving
	for i , v in ipairs(Player.Backpack:GetChildren()) do
		if v:IsA("Tool") then
			table.insert(tools,v.Name)
		end
	end
	
	local worked , err = pcall(function()
		DataStore:SetAsync(Player.UserId,tools)
	end)
	
	if not worked then warn(err) end
end)

-- BindToClose would go here
1 Like

Thank you so much sir im gonna try this

it actually doesn’t work i am using this to save from when buying in the shop, so when you buy the next time you join the item is still in your inv that you bought from the shop.

The trick to saving efficiently using DataStores is to store the local player’s data as cache somewhere on the server. So as the local player leaves the game, you don’t have to get save data from the player and instead, you can save the already stored data of the player on the server. This prevents any data losses that might happen when the player leaves before all the data is received from them.

One way is to make a table variable on the server and name it something like “cache.” Thereafter, on the server, run a loop to constantly get player data and store it in the variable that you created. I suggest you do this by using a remote function. Finally, at the end of the server script, using the PlayerRemoving event, you can extract the data specific to that player from the cache variable and save it using the appropriate DataStore function.

Hope it helps!