Hello, So I have a script that saves a players tools, but the tool that is equipped when they leave doesn’t save, I need help on fixing this, here is the script
local ds = game:GetService("DataStoreService"):GetDataStore("ToolSave")
game.Players.PlayerAdded:connect(function(plr)
local key = "id-"..plr.userId
pcall(function()
local tools = ds:GetAsync(key)
if tools then
for i,v in pairs(tools) do
local tool = game.ServerStorage.Tools:FindFirstChild(v)
if tool then
tool:Clone().Parent = plr:WaitForChild("Backpack")
tool:Clone().Parent = plr:WaitForChild("StarterGear")
end
end
end
end)
end)
game.Players.PlayerRemoving:connect(function(plr)
local key = "id-"..plr.userId
pcall(function()
local toolsToSave = {}
for i,v in pairs(plr.Backpack:GetChildren()) do
if v then
table.insert(toolsToSave,v.Name)
end
end
ds:SetAsync(key,toolsToSave)
end)
end)
I most likely did it wrong because it didnt work, but heres what I did
local ds = game:GetService("DataStoreService"):GetDataStore("ToolSave")
game.Players.PlayerAdded:connect(function(plr)
local key = "id-"..plr.userId
pcall(function()
local tools = ds:GetAsync(key)
if tools then
for i,v in pairs(tools) do
local tool = game.ServerStorage.Colas:FindFirstChild(v)
if tool then
tool:Clone().Parent = plr:WaitForChild("Backpack")
tool:Clone().Parent = plr:WaitForChild("StarterGear")
end
end
end
end)
end)
game.Players.PlayerRemoving:connect(function(plr)
local key = "id-"..plr.userId
pcall(function()
local toolsToSave = {}
game.Players.LocalPlayer.Character.Humanoid:UnequipTools()
for i,v in pairs(plr.Backpack:GetChildren()) do
if v then
table.insert(toolsToSave,v.Name)
end
end
ds:SetAsync(key,toolsToSave)
end)
end)
Try printing out the whole process so you know where it went wrong. Print out the tools saved, backpack, tools loaded, and anything else that can make you track the process.
I am sorta tired and confused, I am just going to stop developing for now, when are you able to get a device to script on, because I need a bit of help on this, I am still new to lua