When I leave the game it doesn't save my tools, does anyone know how to fix this?
local DataStoreService = game:GetService("DataStoreService")
local namsDataStore = DataStoreService:GetDataStore("namsDataStores")
local Data = {}
local ToolStorage = game.ReplicatedStorage.Tools -- h
game.Players.PlayerAdded:Connect(function(Player)
local playerUserId = "Player_" .. Player.UserId
local success, errormesage = pcall(function()
namsDataStore:GetAsync(playerUserId)
end)
if success then
for _,ItemsSaved in pairs(Data) do
print(i, tostring(ItemsSaved))
local SavedTools = ToolStorage:FindFirstChild(ItemsSaved)
if SavedTools.Name == ItemsSaved then --You need to find the specific name. or else it will find the data.
local BackpackTool = SavedTools:Clone()
local StarterTool = SavedTools:Clone()
BackpackTool.Parent = Player.Backpack
if not Player.StarterGear:FindFirstChild(StarterTool.Name) then
StarterTool.Parent = Player.StarterGear
print("Tools have successfully loaded. :D")
end
end
end
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
local playerUserId = "Player_" .. Player.UserId
for _,Items in ipairs(Player.StarterGear:GetChildren()) do
if Items ~= nil then
if Items:IsA("Tool") then
table.Insert(Data, tostring(Items.Name))
end
local success, errormessage = pcall(function()
namsDataStore:SetAsync(playerUserId,Data)
end)
if success then
print("Data has saved! :D")
else
print("Oh noes, Pop has ate the tools! D:")
warn(errormessage)
end
end
end
end)
