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:
01 | num = script.Parent.num |
02 | local player = game.Players.LocalPlayer |
03 | local mouse = player:GetMouse() |
04 | local radius = player.Character.HumanoidRootPart.HomingRadius |
05 |
06 | mouse.KeyDown:connect( function (key) |
07 | if key:byte() = = 32 and num.Value = = 1 then |
08 | Attack() |
09 | end |
10 | end ) |
11 |
12 | function Attack() |
13 | radius.Touched:connect( function (hit) |
14 | if hit.Name = = "Target" then |
15 | print ( "I'm executing" ) |
16 | --local target = radius.hit |
17 | end |
18 | end ) |
19 | end |
(Also, anyone know how to get the "hit" to use as a variable?)
01 | num = script.Parent.num |
02 | local player = game.Players.LocalPlayer |
03 | local mouse = player:GetMouse() |
04 | local radius = player.Character.HumanoidRootPart.HomingRadius |
05 | local Toggled = false |
06 |
07 | mouse.KeyDown:connect( function (key) |
08 | if key:byte() = = 32 and num.Value = = 1 then |
09 | Attack() |
10 | end |
11 | end ) |
12 |
13 | function Attack() |
14 | radius.Touched:connect( function (hit) |
15 | if hit.Name = = "Target" then |
Add a new variable called, 'Toggle' Add an if statement so when the event is called, It executes it. But then change the variable to true. This will only allow it to be called once. As the if statement will pick it up as: Toggle = true. Not Toggle = false
That almost works. I should phrase my question differently--whenever the Attack() is executed, I want it to sense if it's touching "Target"....but only in the moment when it's executed. Any other time, it shouldn't check if it's touching. This should be able to repeat, too, since Attack() can be done at any time, multiple times in a game.