TweenService tracing doesn't work. Target flies off!?

This yellow target is supposed to follow your character using TweenService to make it move smoothly and with a small delay.
Unfortunately, as shown in the video, this isn’t the case and the target flies off.

local TweenService = game:GetService("TweenService")
local r_service = game:GetService("RunService")

function trace()
    local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, 0, false, 0.1)
    local newPos = {Position = bomb.Position + Vector3.new(bomb.Target.Value.Position.X, bomb.Position.Y, bomb.Target.Value.Position.Z)}
    local move = TweenService:Create(script.Parent, tweenInfo, newPos)
    move:Play()
end

t, s  = r_service.Stepped:Wait()
d = t + 2 - s
while t < d do
    print(t)
    print(d)
    t = r_service.Stepped:Wait()
    trace()
end

Is there anything wrong I missed using this code?

1 Like

Hello. Can you please explain to me what is supposed to happen?


This is the idea. However, as you can see, the target doesn’t flow as great and keeps up with the character.

Using TweenService, this target might move even smoother with an added delay to increase distance when running.

1 Like

It’s because RunService doesn’t fire fast enough in that case so it can be smooth.
I recommend trying another method than RunService.

Well, that might indeed be one of the reasons to do it a different way. Therefore, I wanted to try Tweening (as shown in my first message). Happen to know what’s causing the target to fly off like that?

1 Like

I recommend you to try not to Tween the object but just set it’s Position.

As mentioned, just changing the position results in slow and faltering movement, especially when you try and create a delay using wait(x) yourself.
This is exactly why I was looking for a different way of movement, being TweenService.

After trying that, the target appears to move smoothly, but somehow does not follow my character and instead flies off.
I am looking for a solution for this fly-away behaviour.

1 Like

Yes, but with RunService.RenderStepped it’s smooth.
RunService.RenderStepped fires very fast so it will be smooth.

TweenService is the smoothest you’ll get on the server, RenderStepped being the exact same.

I don’t know if that’s what causes the problem, but the way your code is structured it won’t work as expected either way.
You create a 1-second-long tween:

local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, 0, false, 0.1)

and then repeat it every 1/30th of a second:

while t < d do
    r_service.Stepped:Wait()

I’m not sure if TweenService is the right approach here at all. If you want to use it, try stopping each tween at the beginning of the loop.

1 Like

Constantly creating a new tween isn’t the best way to get smooth movement.

Instead set the position yourself to the goal of the tween. It will be as smooth as your character if you BindToRenderStep instead of using a while loop and waiting for the step to finish.

If you do want the target to lag behind, then you can implement some of the tweening functions yourself in the BindToRenderStep connection.

function easeOutQuad( alpha )
    return 1 - (1 - alpha) * (1 - alpha)
end

game:GetService( 'RunService' ):BindToRenderStep ( 'TargetTrace', 201, function ( deltaTime )
    local alpha = 2 * deltaTime -- tweak to whatever number you want for smoothness
    script.Parent.Position = script.Parent.Position + directionTowardsGoal * easeOutQuad( alpha )
end )

Where directionTowardsGoal is equal to goalPosition - script.Parent.Position. I assume the goal is the character but I couldn’t see anything about the character in your original post.

1 Like

I managed to fix the main problem, being the target flying off, myself.

local newPos = {Position = bomb.Position + Vector3.new(bomb.Target.Value.Position.X, bomb.Position.Y, bomb.Target.Value.Position.Z)}

In this line, it basically took the current position (100, 5, 100) for example and added the Root’s position, giving a position of (300, 5, -20) for example.

Thank you for your time and replies. I will try and work with your suggestions on how to make the target move more efficient now. :+1: