Tool giving when player joins (Datastore)

Hello everyone i using tutorial from AlvinBlox to make a shop system and i have issue on datastore (my mistakes) i want to make when player removing they items saving on table and when player joins the item from table giving to player

Code:

local dataStore = game:GetService("DataStoreService"):GetDataStore("Data")

game.Players.PlayerAdded:Connect(function(plr)
	local s = Instance.new("Folder",plr)
	s.Name = "leaderstats"
	
	local xrenb = Instance.new("NumberValue", plr.leaderstats)
	xrenb.Name = "Cookie"
	xrenb.Value = 0

	local selled = Instance.new('NumberValue', plr.leaderstats)
	selled.Name = "Cash"
	selled.Value = 5000

	local data

	local success, errorMessage = pcall(function()
		data = dataStore:GetAsync(plr.UserId)
	end)
	if data ~= nil then
		for _, toolName in pairs(data) do
			local tool = game.ReplicatedStorage:FindFirstChild(toolName)
				if tool then
				local newTool = tool:Clone()
				newTool.Parent = plr.Backpack
			

				local newTool = tool:Clone()
				newTool.Parent = plr.StarterGear
			end
		end
	end	
end)	

game.Players.PlayerRemoving:Connect(function(plr)
	local toolsTable = {}

	for _, tool in pairs(plr.Backpack:GetChildren()) do
		if game.ReplicatedStorage.Tools:FindFirstChild(tool.name) then
			table.insert(toolsTable,tool.Name)
		end
	end

	local success, errorMessage = pcall(function()
		dataStore:SetAsync(plr.UserId, toolsTable)
	end)

end)

game:BindToClose(function()
	for _, plr in pairs(game.Players:GetPlayers()) do
		local toolsTable = {}

		for _, tool in pairs(plr.Backpack:GetChildren()) do
			if game.ReplicatedStorage.Tools:FindFirstChild(tool.name) then
				table.insert(toolsTable,tool.Name)
			end
		end

		local success, errorMessage = pcall(function()
			dataStore:SetAsync(plr.UserId, toolsTable)
		end)
	end
end)

Just to be clear, what was the actual issue with your script? Did data not save? Was it erroring?

local dataStore = game:GetService("DataStoreService"):GetDataStore("Data")
local players = game:GetService("Players")
local replicatedStorage = game:GetService("ReplicatedStorage")
-- Players and ReplicatedStorage are defined here because they were utilized multiple times in the script 
-- (a few edits I made removed the second ReplicatedStorage)
-- Functions are better to utilize because if for some reason, you need to call it again, you can
local function onPlayerAdded(plr: Player)
	-- For Instance.new(className, parent), don't use the 'parent' argument
	-- It's slower because of the amount of detections Roblox has to do whenever an object's property changes
	local s = Instance.new("Folder")
	s.Name = "leaderstats"
	s.Parent = plr
	
	local xrenb = Instance.new("NumberValue")
	xrenb.Name = "Cookie"
	xrenb.Value = 0
	xrenb.Parent = s -- Just use the 's' variable 

	local selled = Instance.new('NumberValue')
	selled.Name = "Cash"
	selled.Value = 5000
	selled.Parent = s

	local success, output = pcall(function()
		-- You don't have to create another variable for fetched data, you can just return it to the
		-- 'output' variable
		return dataStore:GetAsync(plr.UserId)
	end)

	if not success then
		-- You should let your script make you knowledgable of any possible issues
		warn("Unable to fetch data:", output)
	else
		if output ~= nil then
			for _, toolName in output do
				local tool = replicatedStorage:FindFirstChild(toolName)
				
				if tool then
					local newTool = tool:Clone()
					newTool.Parent = plr.Backpack
			

					local newTool = tool:Clone()
					newTool.Parent = plr.StarterGear
				end
			end
		end
	end
end

local function onPlayerRemoving(plr: Player)
	local success, errorMessage = pcall(function()
		dataStore:UpdateAsync(plr.UserId, function(oldData)
			-- This acknowledges any existing data inside the player's inventory instead of overwriting it
			local newData = oldData or {}

			for _, tool in plr.Backpack:GetChildren() do
				-- Your previous check didn't mean much; when the data was loaded, 
				-- the script would check if the tool existed before loading it
				-- into the player's inventory
						
				-- Also, I'm not sure how your game operates, so I'm going to operate under the assumption
				-- that duplicate items can exist inside inventories
				table.insert(newData, tool.Name)
			end
					
			return newData
		end)
	end)
	
	if not success then
		-- Again, your script should tell you any possible issues
		warn("Unable to save data:", errorMessage)
	end
end

players.PlayerAdded:Connect(onPlayerAdded)	
players.PlayerRemoving:Connect(onPlayerRemoving)

game:BindToClose(function()
	for _, plr in game.Players:GetPlayers() do
		task.spawn(onPlayerRemoving, plr) -- Call the 'onPlayerRemoving' function for each player 
		-- instead of copying and pasting
	end
end)

What is this?
изображение_2022-07-09_021703090

I forgot to remove that, just replace it with newData

i tried it don’t giving a tool, i will try a fix this