Saving equipped tools on leaving

im having a issue where tools wont save when they are equipped and the player leaves

save function

local function Save(player: Player)
	local dataKey = 'key_' .. player.UserId


	local data = PlayerData[player]

	local success, result = pcall(function()
		ItemStore:SetAsync(dataKey, data, {player.UserId})
	end)


	if not success then
		error(result)
	end

	warn('Successfully saved', player, 'items to Datastore')
end
2 Likes

we would need to see more code for this

1 Like

adding tools function

local function AddItem(item: Instance, folder: Folder, index: number)

	
	if not data[folder.Name] then

		data[folder.Name] = {}
	end


	if not index then
	
		index = #data[folder.Name] + 1
		
		table.insert(data[folder.Name], index, item.Name)
	end



	item.AncestryChanged:Connect(function(_, newParent)
		
		if newParent ~= folder and folder.Parent ~= nil then
			data[folder.Name][index] = nil
		end
	end)
end


for storeFolderName, savedItems in pairs(data) do


	if onReset == true and storeFolderName ~= 'Backpack' then
		continue
	end


	local itemsFolder: Folder = folders_to_save[storeFolderName]

	
	if itemsFolder then

		
		local folder = folders[storeFolderName]

		for index, itemName in pairs(savedItems) do
			
			local item = itemsFolder:FindFirstChild(itemName)
			if item then
				
				local playerItem = item:Clone()
				playerItem.Parent = folder

				
				AddItem(playerItem, folder, index)
			else
				
				warn(player, 'has unknown item that is said to be in folder', storeFolderName, ":", itemName)
			end
		end

	else
		warn(player, 'has unknown directory saved in data:', storeFolderName)
	end
end
1 Like

I have a working script that does this but I don’t think I should send it to anyone because it is vulnerable to save scumming which is basically where anyone on Roblox can abuse a vulnerability that allows them to revert to a previous data save if they don’t like what they received from RNG

1 Like

so even if you figure out how to do this be careful because it might not be completely safe from vulnerabilities

1 Like

You could just have a datastore which saves the equipped tool, and then just loading that tool once the player rejoins i suppose.

1 Like

Yes that’s what I did and it created a vulnerability where they can undo any of the data the server tried to save

1 Like

so, is there any error messages on Output?

i dont use rng so i wouldnt mind that, ive been like the whole day trying to fix this and never got to a fix