Data Store not Working

My DataSaving system is not working, and I’m trying to save the values in a folder, which is a number value if it makes sense. I also want to save the value of the number value.

Script:

local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("PlayerData")

game.Players.PlayerAdded:Connect(function(plr)
	local stockdata = nil

	pcall(function()
		stockdata = dataStore:GetAsync(plr.UserId.."Stock")
	end)

	if stockdata ~= nil then
		repeat wait(.1) until plr:FindFirstChild("ItemsInStock", true)
		for i, v in pairs(stockdata) do
			if v >= 1 then
				local newVal = Instance.new("NumberValue", plr.ItemsInStock)
				newVal.Name = v.Name
				newVal.Value = v
			end
		end
	else
		return
	end
end)

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

	for _, item in pairs(plr.ItemsInStock:GetChildren()) do
		local newVal = Instance.new("NumberValue")
		newVal.Name = item.Name
		newVal.Value = item.Value
		
		items[newVal.Name] = newVal.Value
	end

	pcall(function()
		dataStore:SetAsync(plr.UserId.."ItemsInStock", pets)
	end)
end)

game:BindToClose(function()
	wait(3)
end)
  1. Where the heck did pets come from?

  2. There’s no need to return at the end of your Players.PlayerAdded callback.

  3. Why create a fresh NumberValue just to store & use already available info?

  4. Instance:WaitForChild exists (line 12). Furthermore, If ItemsInStock not an immediate child of plr, it’s better to travel to its expected location rather than to perform recursive search.

  5. ItemsInStock is an immediate child of plr

  6. You’re wasting a game:BindToClose callback. Please use it to save data. Furthermore, wait has been superseded by task.wait

I used another script from another place, guess I forgot to change it.

Is your game published to web, and studio has api access?