Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

my tool saving script doesn't work when reseting character?

i want to make it so the script can save your tools when u die but it doesnt work help?

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

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()
	end)
end)


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

	local toolsOwned = {}

	for i, toolInBackpack in pairs(plr.Backpack:GetChildren()) do

		table.insert(toolsOwned, toolInBackpack.Name)
	end

	local success, errormsg = pcall(function()

		toolsDS:SetAsync(plr.UserId .. "-tools", toolsOwned)
	end)
	if errormsg then warn(errormsg) end
end)

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(c)
		local h = c:WaitForChild("Humanoid")
		h.Died:Connect(function()
			local toolsOwned = {}

			for i, toolInBackpack in pairs(plr.Backpack:GetChildren()) do

				table.insert(toolsOwned, toolInBackpack.Name)
			end

			local success, errormsg = pcall(function()
				print("Success?")
				toolsDS:SetAsync(plr.UserId .. "-tools", toolsOwned)
			end)
			if errormsg then warn(errormsg) end
			
			local toolsSaved = toolsDS:GetAsync(plr.UserId .. "-tools") or {}

			
		end)
	end)
end)

1 comment

0

Are there any errors?

1 answer

Dude this code is complete mess up! You need to learn more about code mechanics, you will probably learn this as you gain experience but optimize your code! And if you really want to learn to code do not copy code.

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

local toolsFolder = game.ServerStorage.ToolsFolder
local toolTable = {}

local function onCharacterAdded(plr, char)
    repeat wait() until char.Parent
    local hum = char:WaitForChild("Humanoid")
    
    for i, v in pairs(toolTable[plr.UserId]) do
        local tool = v:Clone()
        tool.Parent = plr.Backpack
    end

    local eventConnection
    eventConnection = hum.Died:Connect(function()
        toolTable[plr.UserId] = nil
        hum:UnequipTools()

        for i, v in pairs(plr.Backpack:GetChildren()) do
            table.insert(toolTable[plr.UserId], v)
        end
        eventConnection:Disconnect()
    end)
end

game.Players.PlayerAdded:Connect(function(plr)
    toolTable[plr.UserId] = toolsDS:GetAsync(plr.UserId .. "-tools") or {}

    plr.CharacterAdded:Connect(function(char)
        onCharacterAdded(plr, char)
    end)

    plr.CharacterRemoving:Connect(function(char)
        char.Humanoid:UnequipTools()
    end)
end)


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

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

    if errormsg then warn(errormsg) end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.