Tool save script not working

I have tried making a Datastore Script that saves a player’s tools. For some reason, the player’s tools don’t save. I tried using scripts from youtube hoping that they work. But the same problem occurs with those Scripts.
I also don’t get anything in my output.
(Yes I have “Enable Studio Access to API Services” Enabled)

Here is how far I got with my script:

local dss = game:GetService("DataStoreService")
local toolsDS = dss:GetDataStore("ToolsDatas")


local toolsFolder = game.ServerStorage.Tools

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

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

	print(success)

	print(toolsSaved)

	for i, toolSaved in ipairs(toolsSaved) do
			
		if toolsFolder:FindFirstChild(toolSaved) then 
			
			toolsFolder[toolSaved]:Clone().Parent = plr.Backpack
			toolsFolder[toolSaved]:Clone().Parent = plr.StarterGear 
		end
	end
	

end)


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

	plr.Character.Humanoid:UnequipTools()
	local toolsOwned = {}
	print(plr.Backpack:GetChildren())
	for i, toolInBackpack in pairs(plr.Backpack:GetChildren()) do
		
		table.insert(toolsOwned, toolInBackpack.Name)
	end
	print(toolsOwned)
	local success, errormsg = pcall(function()
		
		toolsDS:SetAsync(plr.UserId .. "-tools", toolsOwned)
	
	end)

		
	if success == false then
		print(errormsg)
	end
	if errormsg then warn(errormsg) end
end)

I would be really happy if somebody could help me.

What about your prints do any of them print? specifically.

print(toolsSaved)

Right, I forgot to mention this.
The tools name do save but it wont load the Tool into the players backpack.

That would mean the source of your problem occurs between these lines but I don’t see anything
wrong.

try change toolSaved to v

for i, toolSaved in ipairs(toolsSaved) do

ToolsSaved Is an array? Right? Because ur using Ipairs I assume it is

1 Like
plr.Character.Humanoid:UnequipTools()
	local toolsOwned = {}
	print(plr.Backpack:GetChildren())
	for i, toolInBackpack in pairs(plr.Backpack:GetChildren()) do

Change to

--plr.Character.Humanoid:UnequipTools() --remove this
	local toolsOwned = {}
	print(plr.Backpack:GetChildren())
	for i, toolInBackpack in pairs(plr.StarterGear:GetChildren()) do

Also you need to used UpdateAsync when player leave to prevent data loss if you don’t handle it when using SetAsync

Edit

1 Like