I have this Folder which holds my inventory, I'm trying to datastore it but I don't quite understand what I'm doing wrong.

Error: "bad argument #1 to 'pairs' (table expected got string)"

ServerScript

local DS = game:GetService("DataStoreService")
local PlrService = game:GetService("Players")


local function onPlayerRemoving(plr)
	
	wait()
	
	local Datastore = game:GetService("DataStoreService"):GetDataStore(plr.Name.."tHats01")
	
	local Data = {}
	
	for _, ValuesToSave in pairs(plr:WaitForChild("Inventory", math.huge):GetChildren()) do
		
		game:GetService("HttpService"):JSONEncode(ValuesToSave)
		
		table.insert(Data, ValuesToSave)
	end
	
	if Data ~= nil then
		
		Datastore:SetAsync(plr.UserId, game:GetService("HttpService"):JSONEncode(Data))
		
	else
		
		print("No assets to save for " .. tostring(plr.Name) .. ".")
		
	end

end

PlrService.PlayerRemoving:Connect(onPlayerRemoving)

local function onPlayerAdded(plr)
	
	local Datastore = game:GetService("DataStoreService"):GetDataStore(plr.Name.."tHats01")
	
	local SavedFiles = Datastore:GetAsync(plr.UserId)

	if SavedFiles ~= nil then
		
		game:GetService("HttpService"):JSONDecode(SavedFiles)
		-- Retrieves Saved values from last time the player left the game.
		for _, LoadValues in pairs(SavedFiles) do
			
				-- now you've loaded the values, do whatever you want.
			end
		
	
	end
	
	
end

PlrService.PlayerAdded:Connect(onPlayerAdded)

game:BindToClose(function()
	-- Just in case to give more time for saving Inventory...
	wait(10)
	
end)

Any Fixes

1 comment

0

??