Help with creating an auto tool equipper

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)```

An equipped tool is put inside the character. So, all you have to do is check if they have a tool equipped and then save that specific tool.

for i, v in ipairs(plr.Character:GetChildren()) do
if v:IsA("Tool") then
-- Save tool to an equipped data store
end
end

can you help a little more with the equipped datastore thing I am really bad with that so can you provide a semi-full script

ok I made my own script and it works,
code if anyone wants it


game:GetService("Players").PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Wait()
	if ds:GetAsync(Player.UserId..'_EquippedTool') then
		local tool = ds:GetAsync(Player.UserId..'_EquippedTool')
		local c1 = game.ServerStorage.Tools:WaitForChild(tool)
		local c = c1:Clone()
		c.Parent = Player.Character
	end
	
	Player.CharacterRemoving:Connect(function(Character)
					ds:SetAsync(Player.UserId..'_EquippedTool', Character:FindFirstChildOfClass("Tool").Name)
			        Character:WaitForChild("Humanoid"):UnequipTools()
			        print("ToolsUnequiped")
			    end)
		end)

tell me if anything can be improved

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.