So I made this game where you can buy rockets and launch to space but there is no normal roblox backpack the problem is that when I try to leave the game with a rocket equipped it saves it to my backpack and just equips the default rocket is there a way to detect when a player leaves, get the tool that they have equipped and load it when they rejoin next time
current backpack saving script
local ds = game:GetService("DataStoreService"):GetDataStore("RocketGameToolSaveSystemTest2")
game.Players.PlayerAdded:Connect(function(plr)
local key = "id_" .. plr.UserId -- fixed the magic character usage
local success, tools = pcall(ds.GetAsync, ds, key)
if success then
if tools then
for _, toolName in pairs(tools) do
local tool = game.ServerStorage.Tools:FindFirstChild(toolName)
if tool then
tool:Clone().Parent = plr:WaitForChild("Backpack")
tool:Clone().Parent = plr:WaitForChild("StarterGear")
end
end
end
else
print("check into tool data of player id: "..plr.UserId.." and name "..plr.Name)
plr:Kick("Please screenshot this kick message")
-- catch the error
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local key = "id_" .. plr.userId
local toolsToSave = {}
for _, tool in pairs(plr.StarterGear:GetChildren()) do
table.insert(toolsToSave, tool.Name)
end
local success, errorMessage = pcall(function()
ds:SetAsync(key, toolsToSave)
end)
if not success then
-- catch error here, if saving fails
print("check into tool data of player id: "..plr.UserId.." and name "..plr.Name)
end
end)```