Portal breaks after a part touches it

Hello I have a portal in my game that teleports you to an arena, along with clearing your backpack and giving you a certain tool, but, my portal keeps breaking whenever a part that isn’t a player touches it. How can I fix this? Here’s the code:

local Players = game:GetService("Players")
local RESET_SECONDS = 2
local isTouched = false

local function removeItems(player)
	local Backpack = player.Backpack
	local Character = player.Character or player.CharacterAdded:Wait()
	local GearInPlayer = player:FindFirstChild("Backpack")
	local GearInCharacter = Character:GetChildren()

	GearInPlayer:ClearAllChildren()
	for _, Tool in pairs(GearInCharacter) do
		if Tool:IsA("Tool") then
			Tool:Destroy()
		end
	end
end

local function Touch(hit)
	if not isTouched then
		isTouched = true

		local player = Players:GetPlayerFromCharacter(hit.Parent)
		local Tool = player.leaderstats.Pan.Value
		local Pos = game.Workspace.TeleLocation

		if player then
			removeItems(player)
			wait(.1)
			local ChosenTool = game.ServerStorage.Pans:FindFirstChild(Tool):Clone()
			ChosenTool.Parent = player.Backpack
			hit.Parent:moveTo(Pos.Position)
			wait(RESET_SECONDS)	
		else
			warn("This portal is on cooldown")
		end
		isTouched = false
	end
end

script.Parent.Touched:Connect(Touch)

Try move this line to

Sorry if code is not formatted correctly I typing on mobile

1 Like

Hmm, I’m sorry but I don’t quite understand what you mean

Edit this function

To

local function Touch(hit)
	if not isTouched then
		isTouched = true

		local player = Players:GetPlayerFromCharacter(hit.Parent)
		
		local Pos = game.Workspace.TeleLocation

		if player then
local Tool = player.leaderstats.Pan.Value
			removeItems(player)
			wait(.1)
			local ChosenTool = game.ServerStorage.Pans:FindFirstChild(Tool):Clone()
			ChosenTool.Parent = player.Backpack
			hit.Parent:moveTo(Pos.Position)
			wait(RESET_SECONDS)	
		else
			warn("This portal is on cooldown")
		end
		isTouched = false
	end
end
1 Like

Thank you very much for helping me fix this.

1 Like