Tool Randomly Disappears Sometimes After it gets Cloned with no Errors

So I’m making a Top Down camera view shooter, where players get teleported into the map when round starts and they have to get the weapons themselves by touching a part (which is a duplicate of the mesh of the tool) and like 8/10 times the script that gives the weapon works perfectly. The script checks if the player already has the tool in their backpack or character so it doesn’t clone it more than once, then it clones it and then equips it. I used to have everything in one script where it removed all tools that weren’t (toolname) which in this case lets call it AK47, and also cloned and equipped the AK47 while checking if player didn’t already have the AK47 with them so it didn’t give them more than one. I thought it was something wrong with the removing tools part of the script so I separated the script into two different scripts, one for cloning/equipping the tool and the other for removing the tools that weren’t the AK47. I ended up removing the “RemovingTools” script all together and left only the cloning/equipping script to check if it was the “RemovingTools” script that wasn’t working properly and after a couple tries of touching the part and cloning and equipping, it eventually once again, just randomly disappeared out of the player’s hands after like 2 seconds. Keep in mind this rarely happens as the script works perfectly 8/10 times so it’s very frustrating because it’s hard to replicate this bug in the first place. When the bug is about to happen, if there’s another player looking at the player touching the part and equipping that weapon, from the other player’s screen that’s looking at the player touch the part, the player that touched the part seems to pick up an invisible gun (it does the AK47 animation idle/shooting animation but there’s no gun in their hand) but from the player that touched the part’s view, they see the actual gun in their hand and but then it disappears after around 1 or 2 seconds and the AK47 idle and shooting animation a long with it.

Like I said I removed the RemovingTools script all together and the bug still happened where the tool randomly disappears sometimes, so I just have to believe that it’s something wrong with the Cloning/Equipping script here it is,

I just don’t even think it has something to do with this script but I just don’t know anymore I’ve been at this for days now lol. I know it could possibly be other scripts in my game but I just highly doubt it as nothing really interacts with the weapons themselves. This is a hard bug to replicate but it does happen enough times for it to be an issue.

Keep in mind that I am still newish to scripting but I think I have the basics down well enough to understand most scripts. This is also my first devforum post so hopefully I made sense as well D:, I would appriciate any help :slight_smile:


In this video I got the bug a couple times but then at the end I didn’t and it does not give any errors or anything, usually the bug doesn’t occur that often but perfect timing for it to happen that many times in a row lol, help.

4 Likes

This usually happens because of tool setup issues. Make sure your tool is set up correctly.

  • If the property RequiresHandle is true, make sure it’s got one or uncheck it.
  • Are collision and anchor properties correct? A part could be falling through the world.
  • Is everything welded together? Just like the last one, a part could be falling through the world.

Let me know if none of these work.
Fizzitix

3 Likes

Thanks for the reply! Yes the property RequiresHandle is true. Also, the collision and anchor properties are also correct, and everything is correctly welded together. This bug rarely happens so I think if it was something to do with those issues it would happen a lot more, but I’m still newish so I could be wrong but I just double checked as i’m typing this and yes the RequiresHandle is true, collision and anchor properties are correct, and everything is welded together.

2 Likes

This doesn’t seem like a thing that a tool would do by itself, I’m sure it’s one of the other scripts in your system. Do a find and replace all search for the keywords
tool:Destroy()

1 Like

And when you do that open the script that seems most likely to you that would cause this issue, and send an embed off it, because I’m almost 100% sure if you have it welded correctly, unanchored, and all the properties correct, then it’s not the tool, it’s some other script.

1 Like

Wait I take that back, line 24 you have an if statement.

existingTool ← When a player equips a tool it goes to their character (and deletes it from their backpack)
so you need to do:

function existingTool(toolName)
  if player.Backpack:FindFirstChild(toolName) or player.Character:FindFirstChild(toolName) then
          return true
  else 
          return false
      end
end

And replace this with the existingtool variable ^

local part = script.Parent
local toolName = "Deagle"

local function existingTool(player, toolName)
	if player.Backpack:FindFirstChild(toolName) or player.Character:FindFirstChild(toolName) then
		return true
	else
		return false
	end
end

part.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player then
		local tool = game.ServerStorage.Weapons:FindFirstChild(toolName)
		if tool then
			local backpack = player:FindFirstChildOfClass("Backpack")
			local character = player.Character

			if backpack and character then
				if not existingTool(player, toolName) then
					local humanoid = character:WaitForChild("Humanoid")

					local clonedTool = tool:Clone()
					clonedTool.Parent = backpack
					clonedTool:WaitForChild("Handle")
					humanoid:EquipTool(clonedTool)
				end
			end
		end
	end
end)

Something like this? i’m not sure

2 Likes

Yes, try that, it might fix it, and either way you should check for the tool in both the plr and character.

1 Like

Okay thanks! I’ll keep checking every now and then to see if it fixed it, and if it doesn’t happen again after a couple tries (since the bug happens so little times) i’ll let you know if it worked lmao. What exactly is the difference of how I had it, and this?

1 Like

So the difference here was that basically, you were checking if the tool was in the backpack.
Now that works unless the tool gets equipped, when a tool gets equipped it removes it from the backpack and puts it in the character model, so if you only checked the backpack, then you could end up duplicating the gun. However this would only save if from future bugs, I don’t think this fix will fix your issue.

1 Like

It’s just kind of hard to see your entire system, cause I’m theorizing this issue isn’t from the equip script, it has to be from elsewhere.

1 Like

Do you have a dedicated script for removing these guns/tools, if so could you show me the code behind that, because it’d probably be in there.

1 Like

Well yes I do but i ended up removing that script that removed the tools when it touched a part because I thought it was that script causing the issue at first, but even with that script being removed entirely, the cloned gun still disappeared sometimes. It’s hard to say where the issue is even coming from because i’ve also changed the entire way the players got the weapons and just made it so players got a gun randomly from the weapons folder inside ServerStorage every time they respawned by cloning and then equipping it, to check if the weapons disappeared too that way, and after hours of testing to see if the guns would do that disappearing bug, it never happened. so my conclusion from that was, that it HAS to be something with the touch part script. Was this conclusion correct? Did that make sense idk if it did

1 Like

Yea that makes sense, so there literally isn’t a script that does tool:destroy() in your game?

1 Like

I also made it so when a player got a kill, if switched to another weapon from the Weapons folder and still no disappearing bug from that either, so it just HAS to be from that touching part cloning script, but I just don’t know, i feel like something must be missing. I don’t want to make the players get the weapons that way, i just did it as a test to see if the weapons disappeared that way but they never did

1 Like

Yes there is, inside The RoundHandler script that i have here

ti = 420 -- Total seconds for the countdown
	repeat
		local minutes = math.floor(ti / 60)
		local seconds = ti % 60
		status.Value = string.format("%02d:%02d", minutes, seconds)
		ti = ti - 1
		wait(1)

		-- Check if countdown reaches 0 or if a playing player has reached 30 points
		local gameEnd = ti == 0
		for i = 1, #playingPlayers do
			local player = playingPlayers[i]
			if player.leaderstats and player.leaderstats.Points then
				if player.leaderstats.Points.Value >= 5 then
					gameEnd = true
					break
				end
			end
		end

		if gameEnd then
			CanTouchOff.Value = "Off"
			-- Unequip tools from all players
			for i = 1, #playingPlayers do
				local player = playingPlayers[i]
				if player.Character then
					local character = player.Character
					local backpack = player.Backpack

					-- Unequip tools from character
					for _, tool in ipairs(character:GetChildren()) do
						if tool:IsA("Tool") then
							tool.Parent = backpack
						end
					end

					-- Unequip tools from backpack
					for _, tool in ipairs(backpack:GetChildren()) do
						if tool:IsA("Tool") then
							tool.Parent = nil
						end
					end
				end
			end

			break -- Exit the repeat loop since the game has ended
		end
	until ti == 0
1 Like

I also have it inside the part itself but this is the script i removed entirely because this was the script i thought must’ve been causing the issue at first, but even removing this script entirely, the disappearing bug still happened.

local part = script.Parent
local toolName = "AK47" -- Replace "ToolName" with the actual name of the tool in ReplicatedStorage

part.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player then
		local backpack = player:FindFirstChildOfClass("Backpack")
		local character = player.Character

		if backpack and character then
			-- Remove existing tools from the backpack
			for _, existingTool in ipairs(backpack:GetChildren()) do
				if existingTool:IsA("Tool") and existingTool.Name ~= toolName then
					existingTool:Destroy()
				end
			end
		end
	end
end)

1 Like

Do this for me

can you add a print statement like

print("Deleted")  ---on line 40
print("Unequipped")--- on line 33 

After it parents to nil
And after it parents to backpack

And run and test and try recreating it.

1 Like

On the cloning/equipping script?

1 Like

On the RoundHandler script

ti = 420 -- Total seconds for the countdown
	repeat
		local minutes = math.floor(ti / 60)
		local seconds = ti % 60
		status.Value = string.format("%02d:%02d", minutes, seconds)
		ti = ti - 1
		wait(1)

		-- Check if countdown reaches 0 or if a playing player has reached 30 points
		local gameEnd = ti == 0
		for i = 1, #playingPlayers do
			local player = playingPlayers[i]
			if player.leaderstats and player.leaderstats.Points then
				if player.leaderstats.Points.Value >= 5 then
					gameEnd = true
					break
				end
			end
		end

		if gameEnd then
			CanTouchOff.Value = "Off"
			-- Unequip tools from all players
			for i = 1, #playingPlayers do
				local player = playingPlayers[i]
				if player.Character then
					local character = player.Character
					local backpack = player.Backpack

					-- Unequip tools from character
					for _, tool in ipairs(character:GetChildren()) do
						if tool:IsA("Tool") then
                           print("Unequip")
							tool.Parent = backpack
						end
					end

					-- Unequip tools from backpack
					for _, tool in ipairs(backpack:GetChildren()) do
						if tool:IsA("Tool") then
                           print("Destroy")
							tool.Parent = nil
						end
					end
				end
			end

			break -- Exit the repeat loop since the game has ended
		end
	until ti == 0
1 Like