Ok, so I have a datastore script and I need to save about 4 things. 2 of them are tables, and the other 2 are numbers. The script is going over the datastore limit, therefore losing data. How should I go about this? Can I add a wait to the player.removing/ bindtoclose? Here's the code, feel free to edit it for it to work.

Players.PlayerRemoving:Connect(function(client)
	local moneyretries = 3
	local bountyretries = 3
	local inventoryretries = 3
	local codesretries = 3
	local key = "client_" .. client.UserId
	local tools = { }
	local codes = { }
	local money
	local bounty
	local codes_folder = ServerStorage.Codes[client.Name]
	local inventory_folder = inventories[client.Name]
	
	for _, item in ipairs(inventory_folder:GetChildren()) do
		table.insert(tools, item.Name)
	end
	
	for _, code in ipairs(codes_folder:GetChildren()) do
		table.insert(codes, code.Name)
	end
	
	repeat
		local succ, inventory = pcall(function()
			player_data:UpdateAsync(key, function(prev)
				return tools
			end)
		end)
		inventoryretries = inventoryretries - 1
		wait(.1)
	until inventoryretries == 0 or succ
	
	repeat
		local succ, codes = pcall(function()
			player_data:UpdateAsync(key, function(prev)
				return codes
			end)
		end)
		codesretries = codesretries - 1
		wait(.1)
	until codesretries == 0 or succ
	
	money = client.leaderstats.Money.Value
	bounty = client.leaderstats.Bounty.Value
	
	repeat
		local succ, money = pcall(function()
			money_data:UpdateAsync(key, function(prev)
				return money
			end)
		end)
	
		moneyretries = moneyretries - 1
		wait(.1)
	until moneyretries == 0 or succ
	
	repeat
		local succ, bounty = pcall(function()
			bounty_data:UpdateAsync(key, function(prev)
				return bounty
			end)
		end)
		bountyretries = bountyretries - 1
		wait(.1)
	until bountyretries == 0 or succ

	inventory_folder:Destroy()
	codes_folder:Destroy()
	
	inventoryretries = 3
	moneyretries = 3
	codesretries = 3
	bountyretries = 3
	warn("datastore save finished")
end)

1 comment

0

Im not sure a good solution. I personally can only say i use roughly 30 or so scripts that save one thing each. So as the player leaves 30 scripts run essentially, i have yet to have problems, but i can imagine one day i could also reach a limit. If you arent saving too many things, it might he easier to use a similar route.