[SOLVED] Spectate script (Realtime TweenService)

Hello all!

I’m currently scripting a Spectate script to spectate players when you die, and I want to tween the camera’s Cframe to a player. I know how to make the camera jump to a player, but this is what I have for a live tween:

-- Testing live tween with two parts named 'test' and 'destination'
ts = game:GetService('TweenService') 
while wait() do
    anim = ts:Create(workspace.test, TweenInfo.new(0.5, Enum.EasingStyle.Quad), {['CFrame'] = workspace.destination.CFrame}) 
    anim:Play() 
end

This method also won’t stop even if not in a loop, because if the player keeps moving away, it will keep having to chase them.

So, my question: what’s an efficient way to tween the camera to a player and update course when the player moves, all within a certain time frame?

I want this to happen within 0.5 seconds.

Thanks in advance!

Just set the CameraSubject to the Character’s humanoid.

1 Like

I want the camera to interpolate, or tween, between the different players, not instantly jump to them.

1 Like

@Blokhampster34, maybe you could replace the wait() in the loop with a variable that will turn true if a player is being spectated

1 Like

Try this then:

while wait(0.5) do
    anim = ts:Create(workspace.CurrentCameta, TweenInfo.new(0.5, Enum.EasingStyle.Quad), {CFrame = workspace.destination.CFrame}) 
    anim:Play() 
end
1 Like

Yes, that’s a good idea! I could use a magnitude check to see if they are close enough to the player or not, if so, snap to them

1 Like

Sure. It depends how you want it to work.

1 Like

That’s just going to do the same thing that my example did, but with less accuracy

1 Like

Actually, now that I think about it, this would probably work better if the cframe was lerped, wouldn’t it? Because the fractional value would be different every time the end cframe updates. I’d just have to use a small alpha value each time so it looks cleaner.

1 Like

I think tweening should be better since you can also do lots of tweening styles to give the camera a great interpolation to the player. Although, you would have to write more lines of code more for tweening. But it’s a big payoff in the end in my opinion :+1:

2 Likes

Yeah, that’s why I started with tweenservice. I would be missing out on a lot of styles, but I just can’t figure out a reliable way to do it that always functions within a timespan. I wonder if I could lerp it using an alpha value that was tweened with tweenservice, though…? That might make it easier?

1 Like

You could make the script wait for the tween to get finished by doing; tween.Completed:Wait()

2 Likes

Yeah, I could. But wouldn’t the tween finish at an old position? That’s the issue I’m having. When I use tweenservice, it ends up at a position that the player used to be at, but they moved. When I run a loop of tweens, every tween gets overrided by a new one, which means tween.Completed will never fire. Even if it did, there’s the chance that the player moved afterwards, meaning the camera will jump.

Maybe I could alter the time the tween lasts for, so it ends up closer to the player? If I do speed = distance / time, I could get the time left over by the speed of the camera tween and the magnitude between the camera and the player’s torso, maybe.

1 Like

You could yes. But maybe there’s a way to stop the old tween in some way. I haven’t gotten really far with tweening myself but there should probably be some way to stop the last tween from going when the player is moving.

I’m going to see if I can make something that works!

2 Likes

Ok. I’m also writing a script that will (hopefully) tween to a part connected to a motor using a s=d/t calculation. I’ll reply when something happens

2 Likes

One question though. Do you want the camera to be able to rotate manually around the player? Since that’s a bit harder than just following the player.

2 Likes

I’d like the camera to have controls like the normal player would, but only after the camera tweens over to them. Once the camera tweens and latches on, the player can orbit around them

1 Like

I have no idea why you’re using a while wait() do loop when you can employ a RunService.Stepped or similar method.

I was just using that as an example. I normally use RenderStepped

I just made one of my own and it worked fine. There’s not really any overwriting tweens or anything.

https://gyazo.com/ff4e610b30fd0aeac35969f8324715dc