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?
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
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)