I want to create a SaveInventory Script that saves your tools when leaving and rejoining. While it works the only issue I want to solve is that when the player resets the tool is still there. I want the tool to load only when you join the game. Otherwise, if you reset your character you should lose the tool.
local ds = game:GetService("DataStoreService"):GetDataStore("ToolSave")
game.Players.PlayerAdded:connect(function(plr)
local joined = Instance.new("IntValue")
joined.Parent = plr
joined.Name = "ToolSave"
joined.Value = 1
task.wait(2)
if joined.Value == 1 then
joined.Value = 0
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.Weapons:FindFirstChild(v)
if tool then
tool:Clone().Parent = plr:WaitForChild("Backpack")
tool:Clone().Parent = plr:WaitForChild("StarterGear")
end
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)
tool:Clone().Parent = plr:WaitForChild("StarterGear")
There’s your smoking gun.
StarterGear is what the game instances into your backpack everytime your character loads, so putting an item in there does the give-on-every-reset effect.
Yes, like I said above it works, but when I reset the tools are still there. I want the tools to load only when the player joins the game and I have tried to do that and it doesn’t seem to work.
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)
elseif plr.Character:FindFirstChildOfClass("Tool") then
local EquippedTool = plr.Character:FindFirstChildOfClass("Tool")
table.insert(toolsToSave, EquippedTool.Name)
end
end
ds:SetAsync(key,toolsToSave)
end)
end)