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