Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to Abort RocketPropulsion?

Asked by 5 years ago
Edited 5 years ago

I am trying to make a part (rocketPart) follow the target part, and once the rocketPart is in contact with the target part, (or inside the target radius) it is supposed to abort. After a second, the rocketPart will fire again. I tried to use this:

local rocketPart = script.Parent
wait(5)
rocketPart.RocketPropulsion:Fire()
if rocketPart.RocketPropulsion.ReachedTarget() then
rocketPart.RocketPropulsion:Abort()
wait(1)
rocketPart.RocketPropulsion:Fire()
end

But the rocketPart is not aborting. Help?

2 answers

Log in to vote
1
Answered by
noammao 294 Moderation Voter
5 years ago
Edited 5 years ago

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() -- waits until the part has reached is target and then continues executing the code.
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:

--code with 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

0
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. Kaexotix 57 — 5y
0
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. noammao 294 — 5y
0
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. noammao 294 — 5y
0
It probably didn't stop because you didn't need to specify the rocket's target, I already did. Kaexotix 57 — 5y
View all comments (13 more)
0
Glad to hear! : ) don't forget to press "Accept Answer" at the bottom of BlueGrovyle's answer. noammao 294 — 5y
0
How can I loop it so that it doesn't abort only once? Kaexotix 57 — 5y
0
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. noammao 294 — 5y
0
My Discord doesn't work currently, I'll accept this answer Kaexotix 57 — 5y
0
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. noammao 294 — 5y
0
Still doesn't work. Here's a screen-recording of what happened: https://youtu.be/Dpf_6tzilAo Kaexotix 57 — 5y
0
I think you have the video in private. It says it's not available. noammao 294 — 5y
0
Oops, sorry, I made it unlisted now Kaexotix 57 — 5y
0
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... noammao 294 — 5y
0
... 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. noammao 294 — 5y
0
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. noammao 294 — 5y
0
So it is just a bug? Kaexotix 57 — 5y
0
I would assume. Or perhaps that that's the way it's supposed to work. noammao 294 — 5y
Ad
Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

ReachedTarget is an event.

local rocketPart = script.Parent
wait(5)
rocketPart.RocketPropulsion:Fire()
rocketPart.RocketPropulsion.ReachedTarget:Connect(function()
    rocketPart.RocketPropulsion:Abort()
    wait(1)
    rocketPart.RocketPropulsion:Fire()
end)
0
That won't work. You forgot that in a "Connect function" it needs to have a parenthesis at the end. "end)" noammao 294 — 5y
0
Even with the parentheses, it still doesn't work. "14:59:39.557 - Workspace.Part.Script:4: attempt to call field 'ReachedTarget' (a userdata value)" Kaexotix 57 — 5y
0
After some experimenting, I was able to get a script that worked. I believe this answer helped the most. Kaexotix 57 — 5y
0
If BlueGrovyle's answer helped you the most then don't forget to submit his answer as the correct one. Otherwise his answer will not get shown as a good/correct answer and he won't get any points I believe. noammao 294 — 5y

Answer this question