So the reason your rocketpropulsion isn't aborting is because your script will check to see if it is within the range of the target object almost immediately after the "Fire()" function is activated. Thus, most of the times it won't have had the time to reach the target. There are a few ways to go about checking but this one worked for me althought it might not be ideal:
local RocketPropulsion = Instance.new("RocketPropulsion", script.Parent)
RocketPropulsion.Target = game.Workspace.Target
RocketPropulsion.TargetRadius = 5
wait(2)
RocketPropulsion:Fire()
RocketPropulsion.ReachedTarget:Wait()
RocketPropulsion:Abort()
One thing to keep in mind is that if your target part is big (for example, has a size of (10,10,10)) and your TargetRadius is small. For example TargetRadius = 1. Then the ReachedTarget condition will never be met since the location of an object is in it's centre.
An important thing you could have done to improve your code:
local rocketPart = script.Parent
wait(5)
rocketPart.RocketPropulsion:Fire()
if rocketPart.RocketPropulsion.ReachedTarget() then
rocketPart.RocketPropulsion:Abort()
wait(1)
rocketPart.RocketPropulsion:Fire()
end
is that you could have used a loop (while or repeat loop), or a function:
local rocketPart = script.Parent
wait(5)
rocketPart.RocketPropulsion:Fire()
RocketPart.RocketPropulsion.ReachedTarget:Connect(Function()
rocketPart.RocketPropulsion:Abort()
end)
Now I know that I'm not the best programmer and perhaps not even decent. And I may have made this more complicated than it has to be but this is how I would go about doing it.
Also, in your code, it only checks if the part has reached it's target once. So to check it a lot of times and see if the part has reached it's target you could have used loops.
while wait(1) do
local RocketPropulsion = Instance.new("RocketPropulsion", script.Parent)
RocketPropulsion.Target = game.Workspace.Target
wait(1)
RocketPropulsion:Fire()
RocketPropulsion.ReachedTarget:Wait()
RocketPropulsion:Abort()
RocketPropulsion:Destroy()
end
--or if you want to to loop X amount of times:
local x = 1
local loopThisManyTimes = 3
while wait(1) do
print(x)
if x <= loopThisManyTimes then
local RocketPropulsion = Instance.new("RocketPropulsion", script.Parent)
RocketPropulsion.Target = game.Workspace.Target
wait(1)
RocketPropulsion:Fire()
RocketPropulsion.ReachedTarget:Wait()
RocketPropulsion:Abort()
RocketPropulsion:Destroy()
else break
end
x = x + 1
end
17 comments
I don't understand why you changed the target radius for the first script, the "R" on line 6 shouldn't be capitalized, and the rocketPart doesn't even stop anyway.
I changed the target radius because I wanted to increase the distance at which the "reached target" event passes. The default value for target radius is 4, so you can either remove that piece of code completely or change it from 5 to 4. And my bad on the capitalization. I'll change it right away.
Also how come it doesn't stop? It worked perfectly fine when I tried it. In my script , the parent of the script is the Part which is supposed to fly towards another part (game.Workspace.Target) "Target" is just the name of the part.
It probably didn't stop because you didn't need to specify the rocket's target, I already did.
Glad to hear! : ) don't forget to press "Accept Answer" at the bottom of BlueGrovyle's answer.
How can I loop it so that it doesn't abort only once?
The methods fire, abort & reachedtarget only work once when I tried at least. So I had to create new RocketPropulsions in each loop. I'll post my version if you please accept an answer. I know it sounds desperate but it's annoying.
My Discord doesn't work currently, I'll accept this answer
Wait what.. I thought you said you prefered BlueGrovyle's answer. Either way, I'll paste the loop code in my answer at the bottom.
Still doesn't work. Here's a screen-recording of what happened: https://youtu.be/Dpf_6tzilAo
I think you have the video in private. It says it's not available.
Oops, sorry, I made it unlisted now
Okay, so I think I know what the issue is. In the code you declare the rocket propulsion in a variable (local RocketPropulsion = script.Parent.RocketPropulsion). and then, in the loop you fire the rocket, wait until it has reached it's target and then abort. Afterwards you delete the RocketPropulsion from your part (RocketPropulsion:Destroy()). Meaning that the next time you declare...
... the variable, it will return nil since you just deleted it. So what I did in my script is that it creates a new rocket propulsion in the begining of the loop and then deletes it in the end. And the reason is because I found that you can only use the ReachedTarget event functions only once for some reason.
So what will happen if you remove the Destroy() function on line 10 is that it will fire but it will only go to the target parts first position and not activate both the reachedtarget and abort after it's first run through the loop. I don't know why.
So it is just a bug?
I would assume. Or perhaps that that's the way it's supposed to work.