:Destroy() does not destroy a tool

Hello, I’m in process of making an equipment giver for security in my roleplay game.

The issue I’m having is the item won’t remove itself from player inventory upon calling an update event. Not sure why is that happening but player inventory stays the same and the inventory does not change the weapons. From the other hand the code above it works just fine. Each time you respawn it will spawn you with selected gun even if it didn’t give you it after interacting with a button (RefreshArsenalEvent)

Server Script

local ServerStorage = game:GetService("ServerStorage")
local Teams = game:GetService("Teams")
local RefreshArsenalEvent = game:GetService("ReplicatedStorage"):WaitForChild("Events"):WaitForChild("RefreshArsenal")

game.Players.PlayerAdded:Connect(function(player)
	warn("Player!")
	player.CharacterAdded:Connect(function(character)
		warn("Character!")
		if player.Team == Teams["Security Department"] or player.Team == Teams["Mobile Task Forces"] then
			local primaryWeapon = ServerStorage:FindFirstChild(player:WaitForChild("Database"):WaitForChild("PrimaryWeaponData").Value)
			local secondaryWeapon = ServerStorage:FindFirstChild(player:WaitForChild("Database"):WaitForChild("SecondaryWeaponData").Value)
			if primaryWeapon and secondaryWeapon then
				local primaryClone = primaryWeapon:Clone()
				local secondaryClone = secondaryWeapon:Clone()
				primaryClone.Parent = player.Backpack
				secondaryClone.Parent = player.Backpack
			end
		end
	end)
end)

RefreshArsenalEvent.Event:Connect(function(player, Weapon)
	local inventory = player.Backpack:GetChildren()
	print(inventory)
	for _, a in pairs(inventory) do
		if a:HasTag("Weapon") then
			a:Destroy()
			print("Removed: ".. tostring(a))
		end
	end
	print(inventory)
	local primaryWeapon = ServerStorage:FindFirstChild(player:WaitForChild("Database"):WaitForChild("PrimaryWeaponData").Value)
	local secondaryWeapon = ServerStorage:FindFirstChild(player:WaitForChild("Database"):WaitForChild("SecondaryWeaponData").Value)
	if primaryWeapon and secondaryWeapon then
		local primaryClone = primaryWeapon:Clone()
		local secondaryClone = secondaryWeapon:Clone()
		primaryClone.Parent = player.Backpack
		secondaryClone.Parent = player.Backpack
	end
end)

Data1 is P90 so player should switch MP7 with P90.
image
But that does not happen

1 Like

No errors?
or anything that can be helpful?

Nope! no errors at all so yeah idk

Anything happens with the player’s character that may cause the guns to be re-added after destorying?

If you’re destroying the tool in a player’s backpack, you have to do it on the Client. Try firing back to the RemoteEvent with something like “DestroyTool” then in a localscript you can write

RefreshArsenalEvent.OnServerEvent:Connect(function(string, currentTool)
     if string == "DestroyTool" then
          currentTool:Destroy()
     end
end)
1 Like

if you destroy it on clients side it will stay on server side tho, i know as that was the issue in here

Sometimes it’s better to do both, just to be safe. Depends.

1 Like

alright, let me boot up studio and try

I did both and server still sees it in players backpack. Is there any other way to get rid of a tool from players inventory then :Destroy()?

:Destroy() should and does destroy a tool.

whatever ur encountering, we can’t really identify without a minimal place file to examine.

2 Likes

Well that does not help at all as I’m not planning to share my project to devforum

Could the weapons possibly not have the “Weapon” tag?
Try giving the tools the tag when you’re giving them to the Player.

2 Likes

The weapons do have Weapon tag as it does print them

Here is a server sided screenshot when playtesting the game

Okey I almost crashed my studio like that

repeat
				tool:Destroy()
			until tool == nil

image

You may be on to something, after adding or a.Name == "MP7" it did remove MP7, just not the secondary which had the weapon tag. Is there a chance that HasTag() may interrupt functionality? as after checking it by name it did remove the item sucessfully

2 Likes

If it’s going to be a free community resource, you could always release your current project as an alpha/beta version and have others send back the fixed bug(s).

Im sure other people wouldn’t mind helping out on it more directly if given credit for it.

I don’t know. But here’s this.

image

I just set this up.

task.wait(15)
print("Firing")

for _,player in ipairs(game.Players:GetPlayers()) do
	for i,v in ipairs(player.Backpack:GetChildren()) do
		if (v:HasTag("A_Tag!!!")) then
			v:Destroy()
		end
	end
end

And the server removed the tool. So :Destroy() does destroy a tool.

 

Make sure the Tag is on the Tool and not the things inside the Tool, obviously it won’t remove that what you want.

If that doesn’t work, maybe it worked and did remove it, but maybe some other script that you have that we do not know about, is adding the same tool back?

1 Like

I have been developing this game since around 2019/2020 and I have been improving it all of the time. It is a really large studio project which got scrapped multiple times and sometimes even copied with Synapse X and posted so I had to take it down.
If the only way for me to get this issue fix is publishing my project to the community which I spend countless hundreds of hours for I rather figuring out another way.

Ooh, mb

Didnt realize it was a whole game lmao

I just read “Im gonna release it on the DevForum” and figured it would be a model or plugin or something