Jittering effect when tweening in a loop

I’ve changed the player character into a part. I’m changing the movement system as well.
I’ve made it so that while the W key is being pressed, the part keeps moving in the x-axis. The part is moving via a tween.
Everything is working fine except the fact that whenever the W key is pressed, a slight jittery effect is visible. Is it possible to remove this effect?

I’m sorry if the code is too naive; I’m just starting out, so the code might seem a bit disorganized and inefficient. If there’s a better way to achieve this, do let me know. Any help is appreciated.

-- Disabling Player Movement
local LocalPlayer = game:GetService("Players").LocalPlayer
local Controls = require(LocalPlayer.PlayerScripts.PlayerModule):GetControls()
local Tween = game:GetService("TweenService")
Controls:Disable()
-------------
local CAS = game:GetService("ContextActionService")
local character = script.Parent:WaitForChild("HumanoidRootPart")
local press = false
local function moveX(name,state,action)
	if state == Enum.UserInputState.Begin then
		press = true
		while press == true do
			Tween:Create(character, TweenInfo.new(.05,Enum.EasingStyle.Sine, Enum.EasingDirection.InOut,0,false), {CFrame = character.CFrame * CFrame.new(.5,0,0) }):Play()
			wait()
		end
	elseif state == Enum.UserInputState.End then
		press = false
	end
end

CAS:BindAction("moveForward", moveX , false, Enum.KeyCode.W)
character.Position = Vector3.new(0,2,0)

I think personally for this sort of operation I would use VectorForces as this is creating and playing new tweens lasting 0.05 seconds (50ms), but is waiting for the next heartbeat. So this would depend on your average heartbeat rate in game which can fluctuate. If the tween finished the part will stop, if the tween hasn’t finished it creates a tween conflict, although in this situation is probably preferable if you wanted to really use this method.
Try increasing the tween duration slightly to 0.06.

I don’t understand why are you using Tweens. You can simply add or substract the x position.
However, if you want to remove the jittering, you can use Tween.Completed:Wait() before the wait(). For that, you will have to create a variable that will be set to your tween.

-- replace your current contents of loop with this --
local moveXTween = Tween:Create(character, TweenInfo.new(.05,Enum.EasingStyle.Sine, Enum.EasingDirection.InOut,0,false), {CFrame = character.CFrame * CFrame.new(.5,0,0) })
moveXTween:Play()
moveXTween.Completed:Wait()
wait()

Using vector forces is a much better alternative. Thank you!

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