How can I tween a part while it's inside of a moving object?

I’m trying to tween camera (Scripted to a part) in a moving car, but it lags ang goes outside the car.

Is it about the wrong way or it’s false to use TweenService in that case?
By the way, it works perfectly when car is not moving.

elseif inputkey.KeyCode == Enum.KeyCode.F then
		if lookmode == false and busy == false then
			busy = true
			local tw = TweenService:Create(head,TweenInfo.new(0.7,Enum.EasingStyle.Sine,Enum.EasingDirection.Out),{CFrame = game.Workspace.Car.Main.CFrame:ToWorldSpace(camobjectspace)})
			tw:Play()
			tw.Completed:Wait()
			lookmode = true
			busy = false
			print("lookstart")
		elseif lookmode == true and busy == false then
			busy = true
			local tw = TweenService:Create(head,TweenInfo.new(0.7,Enum.EasingStyle.Sine,Enum.EasingDirection.Out),{CFrame = game.Workspace.Car.Main.CFrame:ToWorldSpace(firstcframe)})
			tw:Play()
			tw.Completed:Wait()
			lookmode = false
			busy = false
			print("lookend")
		end

Can give additional details if needed.

can you please send a video of whatever you’re trying to replicate or the error because it looks like you’re making a camera follow a car, but you’ll need to loop it to make it keep following

1 Like

2024-01-28 21-07-02 here is the video.
When the car stopped, it does what I want. But bugs when I try to do it while car is moving.
And the full code

local UserInputService = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")
local lookmode = false
local busy = false
wait(5)
local head = game.Workspace.Car.Main.Arms.Head
local firstcframe = game.Workspace.Car.Main.CFrame:ToObjectSpace(head.CFrame)
local camobjectspace = game.Workspace.Car.Main.CFrame:ToObjectSpace(workspace.Car.Main.Itself.Inside.LookCam.CFrame)


function input(inputkey, _gameProcessedEvent)
	if inputkey.KeyCode == Enum.KeyCode.W then
		game.ReplicatedStorage.SendSpeed:FireServer(true)
		print("gave gas")
	elseif inputkey.KeyCode == Enum.KeyCode.S then
		game.ReplicatedStorage.SendSpeed:FireServer(false)
		print("brake")
	elseif inputkey.KeyCode == Enum.KeyCode.F then
		if lookmode == false and busy == false then
			busy = true
			local tw = TweenService:Create(head,TweenInfo.new(0.7,Enum.EasingStyle.Sine,Enum.EasingDirection.Out),{CFrame = game.Workspace.Car.Main.CFrame:ToWorldSpace(camobjectspace)})
			tw:Play()
			tw.Completed:Wait()
			lookmode = true
			busy = false
			print("lookstart")
		elseif lookmode == true and busy == false then
			busy = true
			local tw = TweenService:Create(head,TweenInfo.new(0.7,Enum.EasingStyle.Sine,Enum.EasingDirection.Out),{CFrame = game.Workspace.Car.Main.CFrame:ToWorldSpace(firstcframe)})
			tw:Play()
			tw.Completed:Wait()
			lookmode = false
			busy = false
			print("lookend")
		end
	end
end
UserInputService.InputBegan:Connect(input)

I’m taking the worldspace and objectspace for change its position based on the cars current position. But TweenService isn’t using the updated position. How can I achieve that?

The problem here is that it’s trying to tween to the old CFrame of the car, thus, lagging behind. If you simply want your camera to stay on the car, just set camerasubject to the car’s main part

like so:

workspace.CurrentCamera.CameraSubject = workspace.Car.Main

If you still want to tween, I suggest hooking it up to a renderstepped and tweening there to make it follow smoothly. If that’s what you want, I’d be happy to give you another example.

1 Like

Camera isn’t lagging while car is moving. The tweening process of the camera makes it lag, so it didn’t worked.

And I think the reason is because of my camera script. It changes the cframe of camera renderstepped to follow the car and also for special mouse effect.

Renderstepped makes sense, and I have to tween. I would be happy if you could give me an example about that.

I just programmed it by myself, with your solution and it worked.

I used :getValue() and RenderStepped to achieve. No lag.

1 Like

Happy to help! Sorry about not writing an example, I was busy at that current time.

1 Like

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