My Save Tool Script Is Erroring

Hello, my save tool script is not working, and I was wondering if anyone could help me fix it, there is one error (screenshot below) and I need help fixing that, so here is my code and error.

local dss = game:GetService("DataStoreService")

local toolsDS = dss:GetDataStore("ToolsData")

local toolsFolder = game.ReplicatedStorage.ToolsFolder

local function AddPlayer(plr)
	
	local toolsSaved = toolsDS:GetAsync(plr.UserId .. "-tools") or {}
	
	print("This line should at least work")
	local Data

	local success, whoops = pcall(function()
		Data = toolsDS:GetAsync(plr.UserId .. "-tools") or {}
	end)

	if success and Data then
		for i, toolSaved in pairs(toolsSaved) do

			if toolsFolder:FindFirstChild(toolSaved) then 

				toolsFolder[toolSaved]:Clone().Parent = plr.Backpack
				toolsFolder[toolSaved]:Clone().Parent = plr.StarterGear 
			end
		end
	else
		warn("Something happened while obtaining to get the Player's Data: ", whoops)
	end

	plr.CharacterRemoving:Connect(function(char)

		char.Humanoid:UnequipTools()
		local tableOfTools = {}
		for i, v in pairs(plr.Backpack:GetChildren()) do
			table.Insert(tableOfitems,v.Name,#tableOfitems+1)
		end 

		local success, whoops = pcall(function()
			toolsDS:SetAsync(plr.UserId .. "-tools", tableOfTools)
		end)

		if success then
			print("Data saved successfully!")
		else 
			warn(whoops)
		end

	end)
end

game.Players.PlayerAdded:Connect(AddPlayer)

for _, Plr in pairs(game.Players:GetPlayers()) do
	AddPlayer(Plr)
end

You never declared tableOfItems, so it errors when you try to get the length of it.

1 Like

What Should I declare tableofitems as then?

From looking at your script, it looks like you meant to use tableOfTools?

I tried that and it did not work.

I just saw the output box and it says attempt to call a nil value on line 36

table.Insert should be lowercase.

I am now getting this error about the table.insert

Get rid of the 3rd argument in table.insert

1 Like

Would that be the +1 or the name or what would it be

When you do not put in the 3rd argument of table.insert, it will automatically add it to the “end” of the table (so +1).

“Appends the provided value to the end of array t. The optional pos value defaults to #t+1, meaning that value is inserted at the end of array t unless otherwise specified.”

Should I keep the # at the beginning of the second tableoftools

What part of the code are you talking about? Do you mean this:

Yes I do mean that part of code.

First off, you need to make it a lowercase “I” instead. Also, you can instead just do this:

for i, v in pairs(plr.Backpack:GetChildren()) do
	  table.insert(tableOfTools, v.Name)
end 

Edit: You should save on PlayerRemoving instead.

There was no errors, however the saved data successfully did not print

or i, v in pairs(plr.Backpack:GetChildren()) do
			table.insert(tableOfTools, v.Name)
		end 

		local success, whoops = pcall(function()
			toolsDS:SetAsync(plr.UserId .. "-tools", tableOfTools)
		end)

		if success then
			print("Data saved successfully!")
		else 
			warn(whoops)
		end

Edit: The Warn Statement did also not print

You should save data in PlayerRemoving instead. Also, you should not be saving tools from the Backpack. I would recommend storing the tool information in a table instead.

Sorry to keep asking, but i’m new to datastores and I don’t quite get the full concept so I was wondering if I could get an example please.