How to save a character's tool?

I have a script where it says the players backpack items, but it doesn’t saves the player’s held item, how would I do this? here’s my current script:

local DataStore = game:GetService("DataStoreService")
local CurrentData = DataStore:GetDataStore("Items")

game.Players.PlayerAdded:Connect(function(Player)
	local Data
	local success, whoops = pcall(function()
		Data = CurrentData:GetAsync(Player.UserId)
	end)

	if success and Data then

		for _, Tool in pairs(Data) do
			local ToolCheck = game.ReplicatedStorage.Items:FindFirstChild(Tool)
			if ToolCheck then
				local ToolClone = ToolCheck:Clone()
				ToolClone.Parent = Player.Backpack
			end
		end

	end

end)

game.Players.PlayerRemoving:Connect(function(Player)
	local ToolsToSave = {}
	for _, Tool in pairs(Player.Backpack:GetChildren()) do
		table.insert(ToolsToSave, Tool.Name)
	end
	local success, whoops = pcall(function()
		CurrentData:SetAsync(Player.UserId, ToolsToSave)
	end)

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

If you don’t care about having them start back up with the tool in their hand you could just add an UnequipTools before you do the save.

1 Like

Are you sure that would work, the player is leaving so would the player’s character even still be in the workspace?

I have this when loading the characters in my player initialization:

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

It will occur before the player removal.

1 Like

Ok, I just left for school but I will test it when I get home, thanks!

your code isn’t messing any thing just try to add bind Close when the game close this maybe it

local DataStore = game:GetService("DataStoreService")
local CurrentData = DataStore:GetDataStore("Items")

game.Players.PlayerAdded:Connect(function(Player)
	local Data
	local success, whoops = pcall(function()
		Data = CurrentData:GetAsync(Player.UserId)
	end)

	if success then
		Data = Data or {}
		for _, Tool in pairs(Data) do
			local ToolCheck = game.ReplicatedStorage.Items:FindFirstChild(Tool)
			if ToolCheck then
				local ToolClone = ToolCheck:Clone()
				ToolClone.Parent = Player.Backpack
			end
		end

	end

end)

game.Players.PlayerRemoving:Connect(function(Player)
	local ToolsToSave = {}
	for _, Tool in pairs(Player.Backpack:GetChildren()) do
		table.insert(ToolsToSave, Tool.Name)
	end
	local success, whoops = pcall(function()
		CurrentData:SetAsync(Player.UserId, ToolsToSave)
	end)

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

game:BindToClose(function()
    for _ , player in game.Players:GetPlayers() do
         task.spawn(function()
              local ToolsToSave = {}
              for _, Tool in pairs(Player.Backpack:GetChildren()) do
                    table.insert(ToolsToSave, Tool.Name)
             end
             local success, whoops = pcall(function()
                  CurrentData:SetAsync(Player.UserId, ToolsToSave)
              end)

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

         end)
    end
end)
local DataStore = game:GetService("DataStoreService")
local CurrentData = DataStore:GetDataStore("Items")

game.Players.PlayerAdded:Connect(function(Player)
	local Character = Player.Character
	local Tool = Character:FindFirstChildWhichIsA("Tool")

	if Tool then
		local success, whoops = pcall(function()
			CurrentData:SetAsync(Player.UserId, Tool.Name)
		end)

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

game.Players.PlayerRemoving:Connect(function(Player)
	local ToolsToSave = {}
	for _, Tool in pairs(Player.Backpack:GetChildren()) do
		if Tool:IsA("Tool") then
			table.insert(ToolsToSave, Tool.Name)
		end
	end

	local success, whoops = pcall(function()
		CurrentData:SetAsync(Player.UserId, ToolsToSave)
	end)

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

game.Players.PlayerAdded:Connect(function(Player)
	local success, data = pcall(function()
		return CurrentData:GetAsync(Player.UserId)
	end)

	if success then
		if data then
			for _, Tool in pairs(data) do
				local ToolClone = game.ReplicatedStorage.Items:FindFirstChild(Tool):Clone()
				ToolClone.Parent = Player.Backpack
			end
		end
	else
		warn(whoops)
	end
end)

1 Like

I’m back from school, here’s the current script I have:

local DataStore = game:GetService("DataStoreService")
local CurrentData = DataStore:GetDataStore("Items")

game.Players.PlayerAdded:Connect(function(Player)
	local Data
	local success, whoops = pcall(function()
		Data = CurrentData:GetAsync(Player.UserId)
	end)

	if success and Data then

		for _, Tool in pairs(Data) do
			local ToolCheck = game.ReplicatedStorage.Items:FindFirstChild(Tool)
			if ToolCheck then
				local ToolClone = ToolCheck:Clone()
				ToolClone.Parent = Player.Backpack
			end
		end

	end

end)

game.Players.PlayerRemoving:Connect(function(Player)
	local ToolsToSave = {}
	Player.CharacterRemoving:Connect(function(char)
		char:WaitForChild("Humanoid"):UnequipTools()
	end)
	for _, Tool in pairs(Player.Backpack:GetChildren()) do
		table.insert(ToolsToSave, Tool.Name)
	end
	local success, whoops = pcall(function()
		CurrentData:SetAsync(Player.UserId, ToolsToSave)
	end)

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

It doesn’t appear to be working, any thoughts?

I ended up making a script that keeps the last held item, and just made it so it adds to the table.

Just for future reference you would have added it inside the PlayerAdded function not the PlayerRemoving. Sorry I wasn’t specific.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.