Data not saving?

Everything it works, and it’s Printing the First tool saving, but once it’s time to add to the folder the stuff, it says this: 16:36:55.837 - ServerScriptService.Car Handler:36: bad argument #1 to ‘pairs’ (table expected, got no value)

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function()
		local Character = Player.Character or Player.CharacterAdded:Wait()
		Character:WaitForChild("Humanoid").Seated:Connect(function(IsSeated, WhatSeat)
			if IsSeated then
				if WhatSeat.Name == "DriveSeat" then
					if Player.Backpack:FindFirstChild("HK416") then
						Player.Backpack:FindFirstChild("HK416"):Destroy()
					end
				end
			end
		end) --Load
	end)
	local Folder = Instance.new("Folder", game.ServerStorage)
		Folder.Name = Player.Name.."-Trunk"
	local success = pcall(function()
		local data = Data:GetAsync(Player.UserId.."-Trunk") 
	end)
	if success then
print("Hi")
		for i,v in pairs(Data:GetAsync(Player.UserId.."-Trunk") ) do
			if v[1] then
			local Folder2 = Instance.new("Folder", Folder)
			Folder2.Name = v[1]
			end
		end
	end
	
end)

game.Players.PlayerRemoving:Connect(function(Player)
	local StuffToSave = {}
	for i,v in ipairs(game.ServerStorage:FindFirstChild(Player.Name.."-Trunk"):GetChildren()) do
		table.insert(StuffToSave, {v.Name})
		print("Saved "..v.Name)
	end
	game.ServerStorage:FindFirstChild(Player.Name.."-Trunk"):Destroy()
	if StuffToSave[1] then
	print(StuffToSave[1])
	end
	Data:SetAsync(Player.UserId.."-Trunk", StuffToSave)

end)

That error means the table you provided for the in pairs loop at line 36 is a nil value. Here’s the main issue I noticed (sorry if this doesn’t solve your problem, I’m on mobile right now):

You use GetAsync twice unnecessarily in the PlayerAdded function. Try replacing the code from the local success = pcall(function() till the end of the PlayerAdded function with this:

local data
local success = pcall(function()
	data = Data:GetAsync(Player.UserId.."-Trunk") 
end)
if success and data then
	print("Hi")
	for i,v in pairs(data) do
		if v[1] then
			local Folder2 = Instance.new("Folder", Folder)
			Folder2.Name = v[1]
		end
	end
end

Which in pairs loop is at line 36 in your script? Can’t tell at the moment.

hey, there’s no error, but It’s just not making Folders now, I tried on Removing the “data” on lua if success and data then
but said table expected got nil, like if there was no data, but it should’ve saved tho.

Hi!

  • Remove your variable “data”
  • Change your “local success = pcall(…” to “local success, data = pcall…”
  • Change your “data = GetAsync(…” to “return GetAsync(…”

See if that works for you.

? Your message is hard to understand

Nvm! It worked! Thank you! 30 chars

1 Like