I have been having problems with saving tools for countless years now. I think that I have finally found the solution. (this script) -
local DatastoreService = game:GetService("DataStoreService")
local RunService = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ToolDataStore = DatastoreService:GetDataStore("tools")
local ToolRefrence = ReplicatedStorage.CurcWeapons
game.Players.PlayerAdded:Connect(function(player: Player)
local savedTools = nil
local success, err = pcall(function()
savedTools = ToolDataStore:GetAsync(player.UserId)
end)
if success then
print("Tools Loaded!")
print(savedTools)
elseif err then
warn(err)
end
local starterGear = player.StarterGear
-- Give's tools based on the saved tool names
if savedTools then
for toolName, _ in pairs(savedTools) do
local toolTemplate = ToolRefrence:FindFirstChild(toolName)
if toolTemplate then
local newStarterGear = toolTemplate:Clone()
newStarterGear.Parent = starterGear
end
end
end
-- When the player dies make sure that the unsaved weapons will still be in the backpack
player.CharacterRemoving:Connect(function()
for _,tool in pairs(player.Backpack:GetChildren()) do
if starterGear:FindFirstChild(tool.Name) == nil then
local clone = tool:Clone()
clone.Parent = starterGear
end
end
end)
if not player:HasAppearanceLoaded() then
player.CharacterAppearanceLoaded:Wait()
end
-- Add tools if not given by starter pack
for _,tool in pairs(player.StarterGear:GetChildren()) do
if player.Backpack:FindFirstChild(tool.Name) == nil then
local clone = tool:Clone()
clone.Parent = player.Backpack
end
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local toolSave = {}
for _, tool in pairs(player.StarterGear:GetChildren()) do
if ToolRefrence:FindFirstChild(tool.Name) then -- Makes sure that the tool is able to be loaded back in
toolSave[tool.Name] = true
end
end
local success, err = pcall(function()
ToolDataStore:SetAsync(player.UserId, toolSave)
end)
if success then
print("Tools Saved!")
print(toolSave)
elseif err then
warn(err)
end
end)
game:BindToClose(function()
if RunService:IsStudio() then
task.wait(3)
else
task.wait(10)
end
end)
As you can see in the screenshot the weapons do load in and are being recognized by the script but I think I might need to add delay somewhere so they have time to parent to the backpack? As of the moment I have to reset my player to load in them in correctly. I had some friends join with me and for them even after resetting some tools were missing. Can someone please finally help me fix this issue after years of trying?