Tool datastore help

so im trying to make a datastore to save tools using strings,

local DSS = game:GetService("DataStoreService")
local weaponDS = DSS:GetDataStore("weaponDS")

local function getWeapons(folder)
	local weapons = folder:GetChildren()
	local weaponTable = {}
	for i,weapon in pairs(weapons) do
		table.insert(weaponTable,weapon.Value)
		return weaponTable
	end
end
--note for me: create a string value when the player gets a weapon
game.Players.PlayerAdded:Connect(function(player)
	local WeaponsFolder = Instance.new("Folder",player)
	WeaponsFolder.Name = "WeaponsFolder"
	
	
	local playerId = player.userId
	local weaponTable = getWeapons(WeaponsFolder)
	local success,err = pcall(function()
		local data = weaponDS:GetAsync(playerId)
		for i,tool in pairs(data) do
			local newString = Instance.new("StringValue",WeaponsFolder)
			newString.Value = data[i]
			newString.Name = data[i]
		end
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
	local weaponTable = getWeapons(player:WaitForChild("WeaponsFolder"))
	local playerId = player.userId
	
	local success,err = pcall(function()
		weaponDS:SetAsync(playerId,weaponTable)
	end)
	if success then
		print("saved")
	else
		print("didnt save")
	end
	
end)

game:BindToClose(function()
	local players = game.Players:GetChildren()
	for i,player in pairs(players) do
		local playerId = player.userId
		local weaponTable = getWeapons(player:WaitForChild("WeaponsFolder"))
		local success,err = pcall(function()
			weaponDS:SetAsync(playerId,weaponTable)
		end)
		if success then
			print("saved")
		else
			print("didnt save")
		end
	end
end)

but it says this:
DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key = 247406010 (x2)

1 Like

Get your datastore do the work first and then wait for its result by the pcall(purpose for it is stop the script(yield) until it returns(recieves) a result from getting the value in datastore etc), otherwise it will fail often.

local data -- undefined variable, will be used later to get the datastore value
local success,result = pcall(function()
	 data = weaponDS:GetAsync(playerId)
end)
if success then -- if the operation succeeded then get the result(could be an error or succesfully retrieved value)
   if result then -- this will either error or have the value
     for i,tool in pairs(data) do
	   local newString = Instance.new("StringValue",WeaponsFolder)
	   newString.Value = data[i]
	   newString.Name = data[i]
     end 
   else warn("No data is retrieved") end
else warn("error getting data") end

I hope this helped, sorry my explanation might not be clear here. Have a great day

2 Likes

that worked but now it prints line 45 and 61 which is

print("didnt save")
1 Like

I dont think you need to use Bind to close to save all players data? Arent you trying to save a certain player’s data as they leave? Im pretty sure its only necessary on Player Removing, not when shutting down the whole game

yea but if the server shutsdown then it wont save thats what bind to close is for

1 Like

could you show me the full error

		print("didnt save",err)

didnt save Argument 2 missing or nil

for both

1 Like

Can you print weaponTable before saving?

1 Like

no when i print it, it prints it as nil

nvm i fixed it i put the return table inside the for loop lmao

1 Like

Then there is an issue with your getWeapons function.

Move the return statement outside the for loop.

1 Like

the table prints like its empty tho
{}
like that

Then your script is working. If it is an empty array, that means you have no data.

1 Like

the warning is still the same though

when i test it in studio the warning exists but when i play the game it loads and save perfectly