Tool saving script won't work

1.What do you want to achieve? A working inventory saver

2.What is the issue? Code won’t work

3.What solutions have you tried so far? Youtube and the DevForum

Here is my code:

local ToolFolder = game:GetService(“ServerStorage”):WaitForChild(“SavedTools”)
local DataStoreService = game:GetService(“DataStoreService”)
local SaveData = DataStoreService:GetDataStore(“SaveData”)

game.Players.PlayerAdded:Connect(function(Player)

local Backpack = Player:WaitForChild("Backpack")
local StarterGear = Player:WaitForChild("StarterGear")

local ToolData
local success, whoops = pcall(function()
	ToolData = SaveData:GetAsync(Player.UserId)
end)

if success and ToolData then
	print("Data successfully loaded!")
	
	for i, v in pairs(ToolData)do
		if ToolFolder:FindFirstChild(v) and Backpack:FindFirstChild(v) == nil and StarterGear:FindFirstChild(v) == nil then
			ToolFolder[v]:Clone().Parent = Backpack
			ToolFolder[v]:Clone().Parent = StarterGear			
		end
	end
	
else
	warn(whoops)
end

Player.CharacterRemoving:Connect(function(Character)
	Character:WaitForChild("Humanoid"):UnequipTools()
end)

end)

game.Players.PlayerRemoving:Connect(function(Player)
local ToolTable = {}
for i, v in pairs(Player.Backpack:GetChildren()) do
table.insert(ToolTable, v.Name)
end

local success, whoops = pcall(function()
	SaveData:SetAsync(Player.UserId, ToolTable)
end)

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

end)
Appreciate it if you could help out thanks!

3 Likes

Are there any errors in the output? If yes, could you show a screenshot of it (or just copy the error text from the output)? When you mean the code doesnt work, do you mean like nothing works at all? It never saves your tools when you leave the game?

ServerScriptService.InventorySaver:12: attempt to index nil with ‘FindFirstChild’ And no it doenst save the tools

Is this the code line that the error is occuring?

1 Like

if ToolFolder:FindFirstChild(v) and Backpack:FindFirstChild(v) == nil and StarterGear:FindFirstChild(v) == nil then yup thats it how do I fix this?

1 Like

So @BabyNinjaTime how would you recommend I fix this?

1 Like

im not a very good scripter but, i think that the pairs is not getting anything from the datastore for some reason

Make sure no values are nil, so I recommend printing ToolFolder, Backpack, and StarterPack before the loop through ToolData, and make sure ToolData is not nil either

1 Like

When I printed tool folder I got nil does that help you?

You need to reference ToolFolder properly. Check it’s directory to see if it’s there

1 Like

Im kind of a noob could you elaborate on that please?

So I see that when you set the value ToolFolder you do

game:GetService(“ServerStorage”):WaitForChild(“SavedTools”)

Check ServerStorage and see if the folder SavedTools is there, if it isn’t add one

1 Like

I finished it @zhenjie2003Alt. There wasn’t one so I added it.

Well it should work now. Try it and if it works please mark me as solution.

1 Like

It doesn’t work @zhenjie2003Alt but it got rid of the error.

Then it’s something else with your code. I can’t help any further right now so either you’re gonna have to figure it out yourself or have someone else help sorry.

1 Like

Are you still having problems or is it fixed?

1 Like

Could you mention or check what exactly is inside ToolData?

1 Like

He puts the name of every tool on the player inside the ToolData

1 Like

Still the same error sadly, I might give up