Removing weapons from a player's loadout?

For my game I’m planning to add a system of weapons that you can customize using a gui I plan to make in the future. I’ve laid the groundwork, my only issue removing weapons. Picking up a weapon off the ground will save it into your inventory, and upon rejoining, it’ll be there. All I need to do is make it so you can remove weapons. The weapons are saved to a datastore, so I could add a button that removes them from the datastore. Issue is, I’m not sure how. I know that :RemoveAsync is a thing, but I have no clue how to use it. Here’s the script if anyone is intrested:

local dss = game:GetService("DataStoreService")

local toolsDS = dss:GetDataStore("ToolsData")

local toolsFolder = game.ServerStorage.ToolsFolder

game.Players.PlayerAdded:Connect(function(plr)
	
	local toolsSaved = toolsDS:GetAsync(plr.UserId .. "-tools") or {}
	
	for i, toolSaved in pairs(toolsSaved) do
			
		if toolsFolder:FindFirstChild(toolSaved) then 
			
			toolsFolder[toolSaved]:Clone().Parent = plr.Backpack
			toolsFolder[toolSaved]:Clone().Parent = plr.StarterGear 
		end
	end
	
	plr.CharacterRemoving:Connect(function(char)
    	
		char.Humanoid:UnequipTools()
    end)
end)


game.Players.PlayerRemoving:Connect(function(plr)
	
	local toolsOwned = {}
	
	for i, toolInBackpack in pairs(plr.Backpack:GetChildren()) do
		
		table.insert(toolsOwned, toolInBackpack.Name)
	end
	
	local success, errormsg = pcall(function()
		
		toolsDS:SetAsync(plr.UserId .. "-tools", toolsOwned)
	end)
	if errormsg then warn(errormsg) end
end)
1 Like

Well, you sorta use :RemoveAsync like SetAsync but just without the parameter to save something within it.
Datastore:RemoveAsync( {The key to access the guns.} ) Without the {}, the {} is just to make it more visible to you

1 Like

It works like a charm, only issue is it removes every weapon in the player’s loadout. I’m not sure if there’s a way around this, maybe with values or something? I’m not sure.

1 Like

Then you’re probably gonna want to use :GetAsync() to get the data, edit the data, then use :SetAsync() to override the previous loadout with the edited loadout. I think that’s what you’re looking for.

1 Like