Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Using TweenService to move a "CameraPart" but movement still appears to be laggy when testing?

So, I have inserted a script into a part that I have called 'CamPart' in Workspace. I have set the local user's camera CFrame equal to this part. This is looped using RenderStepping so that the user's camera will be equal to 'CamPart's newest position as soon as possible. 'CamPart' follows an NPC known as 'farmer'.

The script inside 'CamPart' has the following code:

while wait() do
local tweenService = game:GetService("TweenService")
local farmer = workspace.LocalPlayerScenery.Farmer
local campart = script.Parent

local goal = {}
if farmer.Humanoid.WalkToPoint == workspace.MovementPart.Position then
		campart.Position = farmer.HumanoidRootPart.Position + Vector3.new(6,2.7,0)
		campart.Orientation = farmer.HumanoidRootPart.Orientation + Vector3.new(-10,0,0)
	
	elseif farmer.Humanoid.WalkToPoint == workspace.MovementPart1.Position then
		campart.Orientation = farmer.HumanoidRootPart.Orientation + Vector3.new(-10,0,0)
		campart.Position = farmer.HumanoidRootPart.Position + Vector3.new(0,2.7,6)
		
	elseif farmer.Humanoid.WalkToPoint == workspace.MovementPart1pt2.Position then
		campart.Orientation = farmer.HumanoidRootPart.Orientation + Vector3.new(0,0,-10)
		campart.Position = farmer.HumanoidRootPart.Position + Vector3.new(-6,2.7,0)
	
	elseif farmer.Humanoid.WalkToPoint == workspace.MovementPart2.Position then
		campart.Orientation = farmer.HumanoidRootPart.Orientation + Vector3.new(-10,0,0)
		campart.Position = farmer.HumanoidRootPart.Position + Vector3.new(0,2.7,-6)
	
	elseif farmer.Humanoid.WalkToPoint == workspace.MovementPart2pt2.Position then
		campart.Orientation = farmer.HumanoidRootPart.Orientation + Vector3.new(0,30,10)
		campart.Position = farmer.HumanoidRootPart.Position + Vector3.new(-6,2.7,0)
	
	elseif farmer.Humanoid.WalkToPoint == workspace.MovementPart3.Position then
		campart.Orientation = farmer.HumanoidRootPart.Orientation + Vector3.new(-10,0,0)
		campart.Position = farmer.HumanoidRootPart.Position + Vector3.new(0,2.7,-6)
end
 
local tweenInfo = TweenInfo.new(1)
 
local tween = tweenService:Create(campart, tweenInfo, goal)
 
tween:Play()
end

This is inside a regular script. This part follows an NPC that walks around, and the NPC's legs and arms when moving appear to be slightly laggy/jittery when I test the game.

If anyone has any idea of how to remove/reduce the laggy/jittery movement of the NPC when both 'CamPart' and the camera are following it, please let me know. All help is greatly appreciated. Thank you! :)

1 answer

✓ Accepted answer

First of all, don't loop things that don't need to be looped.

Define your variables outside the loops and only keep the stuff that really needs to be looped on the inside.

Secondly, your goal table has no content. What is it tweening to?

Finally, I'm not exactly sure why you're tweening in a while loop. It seems like you want the camera to follow the campart but you shouldn't tween every frame. However, if the farmer changes his WalkToPoint, the tween would be effective there to smoothly transition the camera to the new angle.

To fix this, we should listen for the changed property signal for WalkToPoint and only then tween the campart.

local tweenService = game:GetService("TweenService")
local localPlayerScenery = workspace:WaitForChild("LocalPlayerScenery")
local farmer = localPlayerScenery:WaitForChild("Farmer")
local farmer_humanoid = farmer:WaitForChild("Humanoid")
local farmerRootPart = farmer:WaitForChild("HumanoidRootPart")
local campart = script.Parent

local tweenInfo = TweenInfo.new(1)
-- we should save the current offsets so we can make allow the camera to follow the farmer at the correct offset
local currentPositionOffset
local currentOrientationOffset
local goal = {}

local transitioning = false -- to allow the campart to tween when walktopoint changes

-- get property changed signal for WalkToPoint
local WalkToPointChanged = farmer_humanoid:GetPropertyChangedSignal("WalkToPoint")

-- whenever WalkToPoint is changed
WalkToPointChanged:Connect(function()
	transitioning = true

	if farmer.Humanoid.WalkToPoint == workspace.MovementPart.Position then
		currentPositionOffset = Vector3.new(6, 2.7, 0)
		currentOrientationOffset = Vector3.new(-10, 0, 0)
	
	elseif farmer.Humanoid.WalkToPoint == workspace.MovementPart1.Position then
		currentOrientationOffset = Vector3.new(-10,0,0)
		currentPositionOffset = Vector3.new(0,2.7,6)
	
	elseif farmer.Humanoid.WalkToPoint == workspace.MovementPart1pt2.Position then
		currentOrientationOffset = Vector3.new(0,0,-10)
		currentPositionOffset = Vector3.new(-6,2.7,0)
	
	elseif farmer.Humanoid.WalkToPoint == workspace.MovementPart2.Position then
		currentOrientationOffset = Vector3.new(-10,0,0)
		currentPositionOffset = Vector3.new(0,2.7,-6)
	
	elseif farmer.Humanoid.WalkToPoint == workspace.MovementPart2pt2.Position then
		currentOrientationOffset = Vector3.new(0,30,10)
		currentPositionOffset = Vector3.new(-6,2.7,0)
	
	elseif farmer.Humanoid.WalkToPoint == workspace.MovementPart3.Position then
		currentOrientationOffset = Vector3.new(-10,0,0)
		currentPositionOffset = Vector3.new(0,2.7,-6)
	end
	
	goal.Position = farmerRootPart.Position + currentPositionOffset
	goal.Orientation = farmerRootPart.Orientation + currentOrientationOffset

	local tween = tweenService:Create(campart, tweenInfo, goal)
	
	tween:Play()
	tween.Completed:Wait()
	transitioning = false
end)

-- to make the campart follow the farmer
while wait() do
	if not transitioning then
		campart.Position = farmerRootPart.Position + currentPositionOffset
		campart.Orientation = farmerRootPart.Orientation + currentOrientationOffset
	end
end

There are a lot of changes that I made in this script so make sure to read it over and understand them! For the lag thing - it was most likely an issue with the tweening every frame rather than actual lag.

Tweens are usually used when you have a start and end destination to allow you to smoothly transition between them. I don't recommend using them at high volumes like this. I hope this helps! :)

3 comments

0

You shouldn't be using wait as a condition, but good answer anyways.

0

why not? its basically equivalent to saying while true do then adding a wait()

0

Thank you so much! Greatly appreciated. I'm going to read over this a few times :)

This discussion is preserved as it was. New answers, comments, and votes are closed.