My Save Tool Script Is not Working

Hello, my save tool script is not working. I was wondering if anyone could help me fix it! Thanks for the help!

Here is my script:

local dss = game:GetService("DataStoreService")

local toolsDS = dss:GetDataStore("ToolsData")

local toolsFolder = game.ServerStorage.ToolsFolder

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

	local toolsSaved = toolsDS:GetAsync(plr.UserId .. "-tools") or {}

	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

	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 
		toolsDS:SetAsync(plr.UserId .. "-tools", tableOfTools)
	end)
end)
3 Likes

First thing: It’s always important to encase your DataStore functions inside pcalls, otherwise the script would have a chance of breaking if the Data attempting to load is returned back as nil

Second thing: Are API Services turned on that should enable the DataStoreService?

local dss = game:GetService("DataStoreService")

local toolsDS = dss:GetDataStore("ToolsData")

local toolsFolder = game.ServerStorage.ToolsFolder

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

    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)
3 Likes

Everything was good except I got this one error.

1 Like

My bad, I forgot to reference the Data

Change line 16 to:

	    for i, toolSaved in pairs(Data) do
2 Likes

Thanks for the help, but there is also this error in the game.

1 Like

Looks like this is line 33:

			table.Insert(tableOfitems,v.Name,#tableOfitems+1)

(Not sure if you meant table.insert, but I’m pretty sure you fixed that)

It looks like there’s no indication of your referencing the tableOfitems variable, did you mean to put tableOfTools instead?

1 Like

I think thats not the fix because the same error still shows.

Not really

Looking at the 2 images closely that you posted:


It could be possible that tableOfitems may still be nil if there’s nothing found inside the Player’s Backpack, maybe you should reference the Player’s StarterGear instead?

1 Like

How should I reference the startergear instead, because I dont know how to add it into the table.insert.

Not sure I follow, can’t you just simply do

		for i, v in pairs(plr.StarterGear:GetChildren()) do
            print(i, v)
			table.insert(tableOfTools,v.Name,#tableOfTools+1)
		end 

?

1 Like

Now theres no error, but no tools are saving should I just stay in the game for a certain amount of time before leaving?

Did you get any print statements? There should’ve been something that printed in the output when you called SetAsync/GetAsync

1 Like

No there is not any print statements in the output.

That wouldn’t make sense though if nothing printed

local function AddPlayer(plr)
    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

There should be something that Outputted as either as print, or a warning message in the Output