DataSave Not saving sometimes?

So i made a storage save system that lets u store items u pick up and abilities u get in ur bag
me and my friends were playing in my game a bit just picking up items and storing abilities
and sometimes data doesnt save when we leave this happends about like once every 5-6 times we leave

i tried making an auto saving feature that auto saves every 120 seconds(2 minutes) but still data is being loss

local DSS = game:GetService("DataStoreService")
local storageStore = DSS:GetDataStore("StorageSavev15")
local httpService = game:GetService("HttpService")
local rps = game:GetService("ReplicatedStorage")
local ss = game:GetService("ServerStorage")
local rs = game:GetService("RunService")
local playersleft = 0



game.Players.PlayerAdded:Connect(function(plr)
	playersleft += 1
	task.wait(0.5)
	local plrdata = plr:WaitForChild("data")
	local data = storageStore:GetAsync(plr.UserId)
	local Slots
	
	rps.StorageReload.OnServerEvent:Connect(function()
		if data then
			local timing = "slot"
			local num = 0
			for i,v in pairs(Slots.auraslots) do
				num += 1
				timing = "slot"..num
				rps.Storage:FireClient(plr,timing,Slots.auraslots[timing])
			end
			timing = "itemslot"
			num = 0
			for i,v in pairs(Slots.itemslots) do
				num += 1
				timing = "itemslot"..num
				rps.Storage:FireClient(plr,timing,Slots.itemslots[timing])
			end
		end
	end)

	if data then
		Slots = httpService:JSONDecode(storageStore:GetAsync(plr.UserId))
	else
		Slots = {
			auraslots = {
				slot1 = "Empty",
				slot2 = "Empty",
				slot3 = "Empty",
				slot4 = "Empty",
				slot5 = "Empty",
			},
			itemslots = {
				itemslot1 = "Empty",
				itemslot2 = "Empty",
				itemslot3 = "Empty",
				itemslot4 = "Empty",
				itemslot5 = "Empty",
			}
		}
	end
	rps.Storage.OnServerEvent:Connect(function(who,arg,ty)
		if who == plr then
			if ty == "item" then
				if Slots.itemslots[arg] == "Empty" then
					
					if plrdata.item.Value == "" then return end
					Slots.itemslots[arg] = plrdata.item.Value
					plr.Character[plrdata.item.Value]:Destroy()
					plrdata.item.Value = ""

					rps.Storage:FireClient(who,arg,Slots.itemslots[arg])
				else
					if Slots.itemslots[arg] then
						if plrdata.item.Value == "" then
							ss.Items[Slots.itemslots[arg]]:Clone().Parent = plr.Backpack
							Slots.itemslots[arg] = "Empty"
						else
							local eh = plrdata.item.Value
							plr.Character[plrdata.item.Value]:Destroy()
							plrdata.item.Value = Slots.itemslots[arg]
							ss.Items[plrdata.item.Value]:Clone().Parent = plr.Backpack
							Slots.itemslots[arg] = eh
						end
						rps.Storage:FireClient(who,arg,Slots.itemslots[arg])
					end
				end
			else
				if Slots.auraslots[arg] == "Empty" then
					if plrdata.Aura.Value == "Auraless" then return end
					Slots.auraslots[arg] = plrdata.Aura.Value
					plrdata.Aura.Value = "Auraless"

					rps.Storage:FireClient(who,arg,Slots.auraslots[arg])
				else
					if Slots.auraslots[arg] then
						if plrdata.Aura.Value == "Auraless" then
							plrdata.Aura.Value = Slots.auraslots[arg]
							Slots.auraslots[arg] = "Empty"
						else
							local eh = plrdata.Aura.Value
							plrdata.Aura.Value = Slots.auraslots[arg]
							Slots.auraslots[arg] = eh
						end
						rps.Storage:FireClient(who,arg,Slots.auraslots[arg])
					end
				end
			end
		end
	end)
	local Bindable = Instance.new("BindableEvent")
	game.Players.PlayerRemoving:Connect(function(plrLeaving)
		if plrLeaving == plr then
			storageStore:SetAsync(plr.UserId,httpService:JSONEncode(Slots))
			Bindable:Fire()
		end
	end)
	rps.AutoSaveRemote.OnServerEvent:Connect(function(autosave)
		if autosave == plr then
			storageStore:SetAsync(plr.UserId,httpService:JSONEncode(Slots))
		end
	end)
	if rs:IsStudio() then
		print("testcomplete")
	else
		game:BindToClose(function()
			task.wait(1)
			while playersleft > 0 do
				Bindable.Event:Wait()
			end
		end)
	end
end)

heres my code ive tried fixing it on my own for about a week now and still cant and it seems to save perfectly fine all the time in studio its only when im testing it in the actual game

1 Like

Use pcall when you use (DataStore,Badges)

ex :

local succes, result = pcall(function()
	return dataStore:GetAsync("TheKey")
end)
------------------------------------------
local succes, response = pcall(function()
	return dataStore:SetAsync("TheKey", TheValue)
end)