Tool Table Saving


local DataStoreService = game:GetService("DataStoreService")

local DataStore = DataStoreService:GetDataStore("CandySave_1")
local serverstorage = game:GetService("ServerStorage")
game.Players.PlayerAdded:Connect(function(player)
	
	local candyFolder = Instance.new("Folder")
	candyFolder.Name = player.Name
	candyFolder.Parent = serverstorage.PlayerThings
	
	local data
	
	local success, errorMsg = pcall(function()
		data = DataStore:GetAsync("CandyData"..player.UserId)
	end)
	
	if success and data then
		for _, ToolName in pairs(data) do
			if serverstorage.PlayerThings[player.Name]:FindFirstChild(ToolName) then
				print(ToolName)
				local clonedTool = game.ServerStorage.Tools:FindFirstChild(ToolName):Clone()
				clonedTool.Parent = game.ServerStorage.PlayerTools:FindFirstChild(player.Name)
				--[
				--local ToolValue = Instance.new("Tool")
				--ToolValue.Name = ToolName
				--ToolValue.Parent = serverstorage.PlayerThings[player.Name]
				--]
			end
			
		end
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local candyTable = {}
	
	for _, candy in pairs(serverstorage.PlayerThings[player.Name]:GetChildren()) do
		table.insert(candyTable, candy.Name)
		print(candy.Name)
	end
	
	local success, errorMsg = pcall(function()
		DataStore:SetAsync("CandyData"..player.UserId, candyTable)
	end)
	
	print("test")
	
	if success then
		print("Saved")
	else
		print(errorMsg)
	end
end)
game:BindToClose(function()
	for _, player in pairs(game.Players:GetPlayers()) do
		local candyTable = {}
		
		for _, candy in pairs(serverstorage.PlayerThings[player.Name]:GetChildren()) do
			table.insert(candyTable, candy.Name)
			print(candy.Name)
		end
		
		local success, errorMsg = pcall(function()
			DataStore:SetAsync("CandyData"..player.UserId, candyTable)
		end)
		
		if success then print("Saved on BTC for "..player.Name) end
	end
end)


DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key = CandyData1186752308.

It won’t save please help. It doesn’t add tool when u join back.

When there’s only 1 player in the game and the player leaves the game it sets their Data and the server also shuts down so it sends another request since you have game:BindToClose which shouldn’t be a problem since it’s only 2 times.

and also the script does nothing if data = nil so it won’t load anything. you should add an “if not data then” inside that it creates a new table and inserts all the tools.

I hope this works for you.