Saving tools and loading em

script be like:

local DataStoreService = game:GetService("DataStoreService")
local RunService = game:GetService("RunService")
local ToolsData = DataStoreService:GetDataStore("ToolsData")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local TempTools = ServerStorage.Tools.TempTools
local CombatTagFolder = ServerStorage.PlayerList.CombatTag


local function savetools (player)
	local toolslist = {}
	
	for i,v in pairs(TempTools:GetDescendants()) do
		if v:IsA("Tool") then
			if player.Backpack:FindFirstChild(v.Name) then
				table.insert(toolslist,v.Name)
			end
		end
	end
	
	print(toolslist)
	
	local Success , Fail = pcall(function()
		ToolsData:SetAsync(player.UserId, toolslist)
	end)
	if not Success then
		ToolsData:SetAsync(player.UserId, toolslist)
	end
end

local function loadtools (player)
	local Data = nil
		
	local Success,Fail = pcall(function()
		Data = ToolsData:GetAsync(player.UserId)
	end)
		
	if Success and Data then
		print(Data)
		for i,v in pairs(Data) do
			if TempTools:FindFirstChild(v.Name,true) then -- read somewhere this is like findfirstdecesndants
				TempTools:FindFirstChild(v.Name,true):Clone().Parent = player.Backpack
			end
		end
	else
		Data = ToolsData:GetAsync(player.UserId)
		
		for i,v in pairs(Data) do
			if TempTools:FindFirstChild(v.Name,true) then
				TempTools:FindFirstChild(v.Name,true):Clone().Parent = player.Backpack
			end
		end
	end
end

game.Players.PlayerAdded:Connect(function(player)
	loadtools(player)
	
	player.CharacterRemoving:Connect(function(character)
		character.Humanoid:UnequipTools()
		
		local tag = CombatTagFolder:FindFirstChild(player.Name)

		if tag then
			tag:Destroy()

			for i,tool in pairs(player.Backpack:GetChildren()) do
				if tool:IsA("Tool") and tool:FindFirstChild("Handle") then
					tool.Parent = game.Workspace
					tool.Handle.CFrame = character.HumanoidRootPart.CFrame + Vector3.new(0,20,0)
				end
			end
		end
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
	savetools(player)
end)

game:BindToClose(function()
	if not RunService:IsStudio() then
		for i,player in pairs(game.Players:GetPlayers()) do
			local save = coroutine.wrap(function()
				savetools(player)
			end)
			save()
		end
	end
end)

output be like:
image

so my issue i think is that when i join the game my tools dont clone from my data not that i have no data

1 Like

I’ll send you some tips, some of them might or might not be the issue;

This is improper error handling, just warn() the error.
Even better have it so that it has a re-try logic.

You could just do player.Backpack:GetChildren() and use that to get the tools, or even better, if you’re putting things inside player.StarterGear, then :GetChildren() on that. It’s safer, it’s over-all just better anyway.

There seems to have a second argument which I was not aware of, but if you’re looking for descendants, you can just use :FindFirstDescendant().

Probably the issue:

Here:

Here you’re trying to index .Name from a string. You have to just to replace that for :FindFirstChild(v) here.


Now reading the part of the script which handles loading, unless you can’t die here, and the character isn’t re-loaded, you’re not loading them inside StarterGear, if you want them to still be in the player’s backpack when they respawn, then do that.

i followed some of the stuff u told me but it dont work still :frowning:

local function savetools (player)
	local tries = 0
	local toolslist = {}
	
	for i,v in pairs(TempTools:GetDescendants()) do
		if v:IsA("Tool") then
			if player.Backpack:FindFirstChild(v.Name) then
				table.insert(toolslist,v.Name)
			end
		end
	end
	
	while tries < 4 do
		print("test")
		local Success , Fail = pcall(function()
			ToolsData:SetAsync(player.UserId, toolslist)
		end)
		
		if Success then 
			print("Saved Data",toolslist)
			break
		else
			print(Fail)
			tries +=1
		end
		wait(0.25)
	end
end

local function loadtools (player)
	local tries = 0
	local Data = nil
	
	while tries < 4 do
		local Success,Fail = pcall(function()
			Data = ToolsData:GetAsync(player.UserId)
		end)

		if Success then
			for i,v in pairs(Data) do
				if TempTools:FindFirstChild(v,true) then -- read somewhere this is like findfirstdecesndants
					local tool = TempTools:FindFirstChild(v,true)
					tool:Clone().Parent = player.Backpack
				end
			end
			
			print("loaded tools",Data)
			break
		else
			tries += 1
			print(Fail)
		end
		wait(0.25)
	end
end

also findfirstdescendant dont work for me
image

Here are a few types to fix your problem:

1 Instead of using

for i, v in pairs(TempTools:GetDescendants()) do

you can simply get all tools of the player by using

for i, v in ipairs(Player.Backpack:GetChildren())

2 You should now “save” all tools name into a table using table.insert(Table, Value).

3 Once you “saved” all tools name, you should save them into a Datastore.

4 Whenever a player join your game, get their saved tools using <DataStore>:GetAsync(Key).

DataStore is the way you define your Datastore.

1 Like