I have a script that executes a function that checks for a touch. However, I only want it to check for it in that moment. Instead, it checks constantly after the command is executed. I'm pretty sure I know why it does this, but I'm sure I would have to check if it's actually touching it in order to do so. How can I get it to only check if it's touching the "Target" when the "Attack()" is executed?
Here's the script:
num = script.Parent.num
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local radius = player.Character.HumanoidRootPart.HomingRadius
mouse.KeyDown:connect(function(key)
if key:byte() == 32 and num.Value == 1 then
Attack()
end
end)
function Attack()
radius.Touched:connect(function(hit)
if hit.Name == "Target" then
print("I'm executing")
--local target = radius.hit
end
end)
end
(Also, anyone know how to get the "hit" to use as a variable?)

2 comments
connect is deprecated in favour of Connect, KeyDown is deprecated in favour of InputBegan and ContextActionService, the former fires for almost all inputs, and who knows maybe one of the UserInputService events/functions may be useful to you: https://developer.roblox.com/en-us/api-reference/class/UserInputService
Btw please don't use global variables. Attack should be declared before the connect call.