Trying to remove these click detectors

Hello yall so I’m trying to use click detectors for targeting enemies in my game however I’ve kind of been struggling with it a bit and need some help

			if button.Name == "Attack" then
				isAttacking = true
				for _,v in pairs(battleFrame:GetDescendants()) do
					if v:IsA("TextButton") then
						v.Visible = false
						if v.Name == "Back" then
							v.Visible = true
						end
					end
				end
				while isAttacking do --decided to put this in a loop so that I can constantly get an updated version of the table if a new enemy or player joins
					local battleData = funcFolder:WaitForChild("battleData"):InvokeServer(battleName)
					if battleData then
						for i,v in pairs(battleData.enemiesInBattle) do
							if workspace.Enemies:FindFirstChild(i) then
								if not game.Workspace.Enemies[i]:FindFirstChild("ClickDetector") then
									local clickDetect = Instance.new("ClickDetector")
									clickDetect.MaxActivationDistance = 500
									clickDetect.Parent = game.Workspace.Enemies[i]
									clickDetect.MouseClick:Connect(function()
										battleFrame.Visible = false
										waitingForPlayersFrame.Visible = true
										local target = clickDetect.Parent
										action:FireServer("Attack",target)
										game.Debris:AddItem(clickDetect,0)
										isAttacking = false
									end)
								end							
							end
						end
					else
						warn("Nil")
						break
					end
					for _,button in pairs(battleFrame:GetChildren()) do
						if button:IsA("TextButton") then
							button.MouseButton1Click:Connect(function()
								if button.Name == "Back" then
									isAttacking = false
									for _,v in pairs(battleFrame:GetDescendants()) do
										if v:IsA("TextButton") then
											v.Visible = true
											if v.Name == "Back" then
												v.Visible = false
											end
										end
									end
								end
							end)
						end
					end
				end

So this is probably unnecessary and long, but it’s what I have lol, but my problem is trying to remove the click detector if the player selects to go back and then flees well the clickdetector is still gonna be inside the enemy so if anyone could maybe help me set this up better that would be cool thanks :slight_smile:

1 Like

Try setting the max activation distance of the click detector to zero. The click detector will still be there but it will be disabled until you give it a max activation distance of at least 1

This is not necessarily the answer you are looking for, but there’s a much better way of clicking on enemies than using a Click Detector. It’s much better to use Mouse events to do so:

In a local script:

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

local EnemyContainer = game.Workspace.Enemies

Mouse.Button1Down:connect(function()
    local obj = Mouse.Target
    if obj.Parent:FindFirstChild("Humanoid") and obj.Parent.Parent == EnemyContainer then
        local Enemy = obj.Parent
        -- do clicking actions here depending on the action type
    end
end)

1 Like

Oh this is a much better solution then what I have it seems like I didn’t even think about doing something like this lol.