Tool constantly cloning to character and destroying

Ok, so I’m trying to make, like, a zone where players hop in and they are given a sword. And the moment they hop out, their sword is gone. Issue is, the sword gets cloned, put to character, and deleted so fast.

I think the problem is in this bit of code:

		inMatch.Changed:Connect(function(value)
			local sword = game.ServerStorage.ClassicSword:Clone()
			
			if value == true and not character:FindFirstChildWhichIsA("Tool") then
				
				sword.Parent = character
			elseif value == false and character:FindFirstChildWhichIsA("Tool") then
				
				character:FindFirstChildWhichIsA("Tool"):Destroy()
			end
		end)

If you don’t think so, please see everything below. me desperate :<

Entirety of my script:
local TweenService = game:GetService("TweenService")

local zone = script.Parent
local matchTrigger = zone.MatchTrigger
local inSound = matchTrigger.In
local outSound = matchTrigger.Out

local charsIn = {}

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local inMatch = character:FindFirstChild("InMatch")
		
		inMatch.Changed:Connect(function(value)
			local sword = game.ServerStorage.ClassicSword:Clone()
			
			if value == true and not character:FindFirstChildWhichIsA("Tool") then
				
				sword.Parent = character
			elseif value == false and character:FindFirstChildWhichIsA("Tool") then
				
				character:FindFirstChildWhichIsA("Tool"):Destroy()
			end
		end)
		
		matchTrigger.Touched:Connect(function(hit)	
			local humanoid = hit.Parent:FindFirstChild("Humanoid")

			if humanoid and humanoid.RootPart == hit and inMatch.Value == false then

				inMatch.Value = true

				table.insert(charsIn, character)

				for i, v in pairs(zone.BeamBases:GetDescendants()) do
					if v.Name == "Top" and #charsIn == 1 then
						TweenService:Create(v, TweenInfo.new(1, Enum.EasingStyle.Quint), {Position = Vector3.new(0,3,0)}):Play()
					end
				end

				inSound:Play()
			end
		end)

		matchTrigger.TouchEnded:Connect(function(hit)
			local humanoid = hit.Parent:FindFirstChild("Humanoid")

			if humanoid and humanoid.RootPart == hit and inMatch.Value == true then
				inMatch.Value = false

				for i, v in pairs(charsIn) do
					table.remove(charsIn, table.find(charsIn, character))
				end

				for i, v in pairs(zone.BeamBases:GetDescendants()) do
					if v.Name == "Top" and #charsIn == 0 then
						TweenService:Create(v, TweenInfo.new(1, Enum.EasingStyle.Quint), {Position = Vector3.new(0,0.5,0)}):Play()
					end
				end
				
				outSound:Play()
			end
		end)
		
		while wait() do
			print(inMatch.Value)
		end
	end)
end)

Because, you’re changing the value within touched events. Touched events are unreliable and will detect touch really quickly.

Highly recommend instead of using touched events, you look into this ZonePlus2 module, by ForeverHD. Pretty sure it utilises regions. Also, the resource shows an example of a use-case identical to yours (giving & removing tools when entering/ exiting).

I’m pretty sure it’s not because of the Touched Events tho. I printed inMatch through a while loop and it, (the printing) turned out fine.

I might be wrong tho so, yeah. :pp

Why don’t you check the backpack of the player as well?

Because I didn’t parent the tool to backpack so It’s not really there.

But can the player unequip it?

1 Like

And also I will recommend doing a for loop so that if the player has multiple copies of the sword they all will be removed.

Exp:
for toolName, toolInstance in pairs(character:GetChildren()) do --"check if tool then remove the tool" end