Saving datastore error (saving tables)

local function save(plr)
	local ALLsave = {}
	local char = plr.Character or plr.CharacterAdded:Wait()
	print("issaving")
	for i, slot in pairs(plr:GetChildren()) do
		print("hello")
		if slot:IsA("Folder") then
			table.insert(ALLsave, {slot.Equipped.Value, slot.Name, "slot", "slotsave", "random", "ez", "common", "alr"})
			for n, folder in pairs(slot:GetChildren()) do
				if folder:IsA("NumberValue") or folder:IsA("StringValue") or folder:IsA("IntValue") then
					--Stats, class
					table.insert(ALLsave, {folder.Value, slot.Name, folder.Name})
				elseif folder:FindFirstChild("exp") then
					for i, item in pairs(folder:GetChildren()) do
						--Level
						table.insert(ALLsave, {item.Name, item.Value, slot.Name, "level", "exp", "max", "levels"})
					end
				end
				for b, item in pairs(folder:GetChildren()) do

					if item:FindFirstChild("Equipped") and item:FindFirstChild("HotbarKey") then
						--Combat Skills
						--Weapons
						table.insert(ALLsave, {item.Name, item.Equipped.Value, slot.Name, item.HotbarKey.Value, item.Parent.Name, item.Upgrade.Value})
					elseif item:FindFirstChild("Equipped") and not item:FindFirstChild("HotbarKey") then
						--Armour
						table.insert(ALLsave, {item.Name, item.Equipped.Value, slot.Name, item.Parent.Name, item.Upgrade.Value})
				
					else
						--Inventory
						table.insert(ALLsave, {item.Name, slot.Name})
					end
				end
			end
		end    
	end
	
	local s, e = pcall(function()
		CharDS:SetAsync(plr.UserId.."Slots", ALLsave)
	end)

	if s then print("Success saving slot for "..plr.Name) else warn(e) print("error saving slots") end
end

game.Players.PlayerRemoving:Connect(function(plr)
	print("Left.")
	save(plr)
end)

This is the part that doesn’t print anything

local s, e = pcall(function()
		CharDS:SetAsync(plr.UserId.."Slots", ALLsave)
	end)

	if s then print("Success saving slot for "..plr.Name) else warn(e) print("error saving slots") end
1 Like

Is there any errors you are getting in Console? If yes, then please share.

local s, e = pcall(function()
		CharDS:SetAsync(plr.UserId.."Slots", ALLsave)
	end)

	if s then print("Success saving slot for "..plr.Name) else warn(e) print("error saving slots") end

Also, this part of code is inside the save() function or outside the save() function? Because there is no other end after the last end of this code, so it seems this isn’t inside the save() function. If it’s inside the function, you should just add another end like this -

local s, e = pcall(function()
		CharDS:SetAsync(plr.UserId.."Slots", ALLsave)
	end)

	if s then print("Success saving slot for "..plr.Name) else warn(e) print("error saving slots") end
end

try using game.BindToClose if its not printing anything. could just be studio.

He is saying that it doesn’t print anything, so I guess it means neither an error or success. Maybe because he didn’t closed the function properly, I mean he didn’t closed till the end.

most likely. but the PlayerRemoving Event could not be firing when stopping game in studio. just want to be sure.

Yes, that may also happen. So to be on a safe side, he should add game.BindToClose event as well.

It prints things inside the actual save script but nothing with the if statement prints. (“left”) prints everything else other than success, or error prints

I also tried game:BindToClose() same thing happens

I also tried putting all the save function inside the player removing function same thing happens

Can you screenshot or copy paste the console? It might give us a clue as to what is happening. Also is studio api on?

image

Can you try printing Allsave before you save it?

Also based on this, check what the slot instance is, none of them are folders. Make sure you are looping through the correct instance. If you can, also screenshot the “slots” while in game.

Loading is fine - it was saving all fine but today it just stopped and idk why I didn’t even change anything

Can you check the slots in the explorer?

Oh yea also can you add an else statement and warn the error?

if s then
—thing
else
warn(e)
end

I did add the else statement - nothing prints and i can check the slots in the explorer iyw

Can I see the code? It has to print either one. If it doesn’t, it means the loop is either erroring or not completely finishing. Add a print before you save the data to check it either is the case.

I printed allSave before the pcall i also added print(“pcall started”) in the pcall and they both print correctly - Everything prints but “success saving” or warn(e) or “error”