How to make an object go to the position of a part?

I’ve currently done it in 2 different ways, and both have worked perfectly

1: I used a While loop to constantly redirect the object to the desired position.
(This while loop runs infinitely until the object reaches the position tolerance.)

2: I used TweenService to make the object go to the exact position.

My question is, what would be better to use, an infinite While loop or TweenService?

Let me explain what I want to do. Let’s say when a player picks up an object, if they take damage or throw it, that object falls to the ground and then returns to its original position.

It’s worth mentioning that the object will be bouncing until it reaches its original position… So what would be the most optimal way to do it?

Example:

1 Like

why not use runservice?, i think it might be more optimized

2 Likes

I agree with @animation_br101 run service would be the best way to go if it’s always constant. Here is some code to help you visualize it:

local runService = game:GetService(“RunService”)

runService.HeartBeat:Connect(function()
–code here
end)

Sorry I’m on phone I’ll change the script to be better when I’m able to get on a computer

2 Likes

@animation_br101 @YoungKnightSoul

Both of the options mentioned by the OP are actually the same, because

while task.wait() do

end

is exactly the same as

RunService.Heartbeat:Connect(function()

end)

For the OP, if you want the object to bounce back to its original position, then I don’t think you should refer to the two options, I believe that will require animations of the object to achieve the “bounce” effect.

1 Like

So, could I use the While loop to make the object always return to the desired position and add a custom animation to the bag so it jumps?

I had implemented “Bounce” effect using the TweenService, but I wasn’t sure if it was the best approach

1 Like

It depends on you. How realistic should the “bounce” effect be? I probably wanna see how the effect looks like before I can give you my opinions.

1 Like

I made this effect using the TweenService:

1 Like

Also if you want it to be more smooth you do:

RunService.RenderStepped:Connect(function()

end)

This would make your script run before the frame is shown

1 Like

The thing is, TweenService only cares about tweening the part’s position property to a goal position with any effects applied on, so even if you use TweenService, when the player picks up the part and walks away from the original position, gets hit or drops the object, it wouldn’t do the bounce effect on the ground, instead it’s going to tween the part to the goal position whilst applying the “bounce” effect, because it will take the goal position as the “ground”.

Here’s a video example to explain it better if you don’t get what I mean.

As you can see when I dropped the part, it didn’t bounce on the ground and went back to the position (assume the original position is the origin). It just snapped the handle part back to the position immediately.

So, if you want to make it so that it bounces until it reaches the original position, my best option is probably animations, or, now that I think of it, you can probably create waypoints along the path, and then try to make the bounce effect to play between two adjacent waypoitns until it reaches the last waypoint (end goal) using PathfindingService.

@YoungKnightSoul
That is completely false. Not only that, it is used inappropriately. RunService.RenderStepped should only be used if you are dealing with the player’s camera or the player’s character model.

2 Likes

I was thinking of using a ‘PathfindingService’ for that same purpose, since different maps could utilize it to ensure the object always returns to the center of the map, and then interpolate the bounces from there… So, by using the ‘PathfindingService,’ would you still recommend using animations for the ‘Bounce’ effect?

Yes. I would still recommend using that service with the bounce effect.

I was busy over the weekend, but today I was finally able to finish it! Thank you very much I used the PathfindingService and at each Waypoint I altered the height to make the Bounce effect.

It was complicated because I only knew how to use it on humanoids, but then with the Tweenservice I was able to make the desired movement between each Waypoint

1 Like

No problem. I am glad my theory worked out. :rofl:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.