Tools keep dissapearing randomly from datastore

a couple of months back i managed to make a system that would save stuff that was inside the character’s backpack, it was working well until recently i’ve been experiencing data loss issues with it, constantly losing my tools and stuff randomly, its really game breaking and i really cant take it anymore
if someone has a solution to this please tell me

Save tools
local success, err = pcall(function()
		for i,v in pairs(player.Backpack:GetChildren()) do
			local Tool = tostring(v.Name)
			table.insert(tableToSave, Tool)
		end
		datastore:SetAsync(player.UserId, tableToSave) 

	end)
Load tools
for i,v in pairs(data) do
				if Items:FindFirstChild(v) and InCombat.Value == false then
					Items[v]:Clone().Parent = player.Backpack
				elseif  Skills:FindFirstChild(v) and InCombat.Value == false then
					Skills[v]:Clone().Parent = player.Backpack
				end
			end

Remember that when players have tools, the tool can be in either their Character (when equipped) or their Backpack. This script simply does not save equipped tools.

thing is, it happens with all tools, i’ve already made it so all tools all unequipped upon leave

Did you try doing that before the save function? It might be unequipping tools after or during saving.

i already said that it refuses to save all tools, not just one

In that case, I can’t figure out what’s wrong. The function should work fine. Probably an issue with some other code.

as i’ve said, it was working perfectly until recently, and i dont remember any updates that deprecated the methods i use

Does this only happen in studio?

it only happens ingame after i get out of studio, i was thinking of disabling datastores inside studio since it seems as everyone else with no studio access doesnt have this problem

Excellent. The fix is simple then.

When you close a game on ROBLOX studio, the server and client stop at the same time, preventing data from saving. This also happens when shutting down servers. To fix it, use this beautiful code from the roblox wiki.

game:BindToClose(function()
    --[[if RunService:IsStudio() then
        return
    end--]]
 
    print("saving player data")
 
    -- go through all players, saving their data
    local players = Players:GetPlayers()
    for i,plr in pairs(players) do
       -- save data
    end
 
    print("completed saving player data")
 
end)

You would’ve also noticed the data save if you switched to server during testing, deleted your player, and waited. But this removes that step.

Something to note: it will take a bit longer for the session to stop, because this function delays the shutdown.

Sorry for the late response, but does studio mess up alot of datastore data and cause stuff like this to happen?

the problem has been fixed, for now even though i suspected that studio was the one making me lose data, thanks alot, i’ll probably make another post if i stumble upon another data loss problem