Starter tool is not deleted after ak47 is being cloned into player inventory

  1. What do you want to achieve? Keep it simple and clear!

Hello, so what am trying to achieve is to only allow players to have a tool at a time. When they interact with the proximity if they have any tool that exist in their inventory. Then it will delete em and clone the Ak47.

  1. What is the issue? Include screenshots / videos if possible!

there is no error in the output. It gives the Ak47 after the player interacted with the proximity but is not deleting the Starter tool that exist in their inventory.

local Ak47 = game.ReplicatedStorage:WaitForChild("Ak47")
local ProximityPrompt = workspace.Sw.ProximityPrompt

ProximityPrompt.Triggered:Connect(function(player)
	if not player.Backpack:FindFirstChild(Ak47.Name) then
		print("Player doesn't have " .. Ak47.Name)
		Ak47:Clone()
		Ak47.Parent = player.Backpack
	else
		print("Player already have " .. Ak47.Name)
		
		for _,v in pairs(game.StarterPack:GetChildren()) do
				if v.Name == "ClassicSword" and not player.Backpack:FindFirstChild(Ak47.Name) then
				print(v.Name .. " has been deleted!")
				v:Destroy()
			end
		end
	end
end)
1 Like

Tools that are equipped go to the character.

Also, should be:

for _,v in pairs(player.Backpack:GetChildren()) do
				if v.Name == "ClassicSword" and not player.Backpack:FindFirstChild(Ak47.Name) then
				print(v.Name .. " has been deleted!")
				v:Destroy()
			end
		end

Because according to the API:

When a player’s character spawns, the contents of the StarterPack and their StarterGear are copied into their Backpack . Once a character dies, the Backpack is removed and a new one is created – populating it using the contents of StarterPack and StarterGear.

1 Like

Could you include the output as well in another video? If I understood what you said correctly then you want there to be only one tool in the backpack at a time. The script makes it look like your trying to have only one AK. Maybe add another variable for backpack? I believe the line would be
Local Backpack = player.Backpack(“Backpack ”)

1 Like

the tool is in StarterPlayer I want it to be deleted if there are any and clone the ak47 into player inventory/backpack

for i, v in pairs(character:GetChildren()) do
if v:IsA("Tool") then
v:Destroy()
end
end

I’m assuming you want the player to have only 1 tool at a time.

local Ak47 = game.ReplicatedStorage:WaitForChild("Ak47")
local ProximityPrompt = workspace.Sw.ProximityPrompt

ProximityPrompt.Triggered:Connect(function(player)
	local a = player.BackPack:GetChildren()
	local b = player.Character:GetChildren()
	local c = {table.unpack(a),table.unpack(b)}
	for i,v in pairs(c) do
		if v:IsA("Tool") then
			v:Destroy()
		end
	end
	local d = Ak47:Clone()
	d.Parent = player.Backpack
end)

Just replace your code with this and it should work.
:slightly_smiling_face:

Edit: did some testing with merging tables, doesn’t work as expected. This may not work.

Hello, can you please explain what does table.unpack means and do??

local sword = character:FindFirstChildWhichIsA("Tool")
if (sword) then
   sword:Destroy()
end

or

character.Humanoid:UnequipTools()
Backpack:ClearAllChildren()
-- give AK

I never knew this function existed, thank you for educating me. Your solution is way less complicated than mine lol (AND IT MIGHT NOT EVEN WORK!!!), I check every instance inside the Character and BackPack to see if there is a tool.

ClearAllChildren() may delete some stuff you wanted to keep though.

1 Like

Try this maybe?

local Ak47 = game.ReplicatedStorage:WaitForChild("Ak47")
local ProximityPrompt = workspace.Sw.ProximityPrompt

ProximityPrompt.Triggered:Connect(function(player)
	for _,v in pairs(game.StarterPack:GetChildren()) do
			if v.Name == "ClassicSword" and not player.Backpack:FindFirstChild(Ak47.Name) then
			print(v.Name .. " has been deleted!")
			v:Destroy()
		end
	end
	if not player.Backpack:FindFirstChild(Ak47.Name) then
		print("Player doesn't have " .. Ak47.Name)
		local Clone = Ak47:Clone()
		Clone.Parent = player.Backpack
		print("Player doesn't have " .. Ak47.Name)
	else
		print("Player already have " .. Ak47.Name)
	end
end)

I tried doing this in localscript but it still didn’t work.

local StarterPack = game:GetService("StarterPack")
local Replicated = game:GetService("ReplicatedStorage")
local Ak47 = Replicated:WaitForChild("Ak47")
local SW = workspace:WaitForChild("Sw")
local Prompt = SW:WaitForChild("ProximityPrompt")

Prompt.Triggered:Connect(function(Player)
	local Character = Player.Character
	local Backpack = Player.Backpack
	local StarterGear
	if Character and Backpack then
		local CharacterTool = Character:FindFirstChild(Ak47.Name)
		local BackpackTool = Backpack:FindFirstChild(Ak47.Name)
		if not (CharacterTool or BackpackTool) then
			local Ak47Clone = Ak47:Clone()
			Ak47Clone.Parent = Backpack
		elseif CharacterTool or BackpackTool then
			local Sword = Backpack:FindFirstChild("ClassicSword")
			if Sword then
				Sword:Destroy()
			end
		end
	end
end)

I provided this in the other thread you posted, this is working for me.

Proof:
https://gyazo.com/402084cbd35d9f190e149a9145786ad7

Here’s the model file.

repro.rbxm (4.4 KB)

When a player equips the tool it will go to the character, so you should put an elseif in your if sword then function.

elseif Character:FindFirstChildOfClass("Tool") then
      Character:FindFirstChildOfClass("Tool"):Destroy()
end

Actually no, I believe the thread’s poster wants the sword to be kept if it is equipped when the “ProximityPrompt” is triggered.

The pleasure is mine to assist a fellow developer! With developers eager to resolve all questions/problems proposed on the devforum, discussing the matter helps with retain desired information.

In regard to the prior usage of ClearAllChildren() in the code written by myself, the simple process of rectification & a miniscule amount of drive of stimulating manner (expectations in mind) will yield results desired.