I am not sure of why this script is not working to save my tools.
Code:
local dataStoreService = game:GetService(“DataStoreService”)
local dataStore = dataStoreService:GetDataStore(“BackpackSave”)
game.Players.PlayerAdded:Connect(function(player)
pcall(function()
local tool = dataStore:GetAsync(“User-”…player.UserId)
if tool then
for i,v in pairs(tool) do
local toolFound = game.ReplicatedStorage.Items:FindFirstChild(v)
if toolFound then
toolFound:Clone().Parent = player.Backpack
toolFound:Clone().Parent = player.StarterGear
end
end
end
end)
end)
game.Players.PlayerRemoving:Connect(function(players)
pcall (function()
local toolsSave = {}
for i, tool in pairs(player.Backpack:GetChildren()) do
if tool then
table.insert(toolsSave,tool.Name)
end
end
dataStore:SetAsync(“User-”…player.UserId,toolsSave)
end)
end)
It would help if you put your code between 3 ` so it is formatted correctly.
Have you tried putting prints in so you can tell where the code is working and not?
Put a folder named “Tools” in serverStorage put the tools your gonna have in your game in to that folder and then put this script in serverScriptService
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)