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:
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
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
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