How could I make my CFrame Script run smoother?

Hello everyone!

I’m trying to fix this script that makes a part follow nodes named in number order using CFrame smoothly. My issue is that the moving between the parts is often jittery. I’m assuming its because it doesn’t move to the next part until the CFrame is EXACTLY the CFrame of the goal. I was hoping to somewhat make it round off the CFrame if this is possible. I’m not super advanced when it comes to these types of things.

Here’s a video of the jittering car (It might be hard to tell from the video)

I would like to point out that the script is running entirely on the server, and not the client. Here’s the script:

local part = script.Parent
local coaster = game.Workspace.Coaster2
local TweenService = game:GetService("TweenService")
part.CFrame = coaster[1].CFrame

while true do
local tweenInfo = TweenInfo.new(
	(script.Speed.Value), -- Time
	Enum.EasingStyle.Linear, -- EasingStyle
	Enum.EasingDirection.InOut -- EasingDirection
	
	)

if  part.CFrame == coaster[script.CurrentNumber.Value].CFrame then
	if coaster:FindFirstChild(script.CurrentNumber.Value+1) then
	script.CurrentNumber.Value = script.CurrentNumber.Value+1
	
	end
	
	local tween = TweenService:Create(part, tweenInfo, {CFrame = coaster[script.CurrentNumber.Value].CFrame})
	tween:Play()

elseif not  coaster:FindFirstChild(script.CurrentNumber.Value+1) then
		script.CurrentNumber.Value = 1
		local tween = TweenService:Create(part, tweenInfo, {CFrame = coaster[script.CurrentNumber.Value].CFrame})
		tween:Play()
end
wait(0)
end

If anyone knows any possible solutions or could point me in the right direction, that’d be very much appreciated. Thanks in advance!

maybe try increasing the value more times by smaller amounts would help?

1 Like

Hmm, I don’t know how this would work considering the nodes are named in number order. That’s my bad, I forgot to put that, going to edit it now!

Okay! Good luck with it :wink: (30 char :>)

1 Like

It’s giving me this error because the names of the parts are whole numbers (First node is 1, second node is 2, ect.)
image
I might have misunderstood you, and I did something wrong.

if coaster:FindFirstChild(script.CurrentNumber.Value+1) then
script.CurrentNumber.Value = script.CurrentNumber.Value+1

above is a part of your script^
i ment you should maybe try changing it to something like this:
if coaster:findfirstchild(script.currentnumber.value +1) then
– repeat this under here 3 times
script.currentnumber.value = script.currentnumber.value + 0.33
wait(0.03)

im quite new to scripting so im not 100% sure this will work

I might be wrong but looking at it, are you doing this on the server? (putting script into workspace?)

Try doing it on the client instead, and if you’re worried about it staying still for other clients, use a remote event and :FireAllClients()

Or try using lerp instead of tweens in a loop.

Yeah, its a server script. To get it from the client to the server would I just fire a remote event that runs the script above? Also, I will try using lerp as well, but I feel like that would mess up the speed,

I’m looking at your script, and it seems like you’re not waiting for the tween to finish

Server scripts are not the right way to go if you’re wanting it to be smooth. You just need to fire to all clients with the same script above (though I recommend using lerp for your use case plus, it shouldn’t mess with the speed, it’ll also probably give you nice acceleration.).

Remember

Client : Effects/Tweens
Server: Important things (lol)

1 Like

it waits until the part has the exact cframe of the goal to move to the next node, which is whats causing the jittering. I was hoping to kind of round it off slightly so once its close to the goal it can just move.

You should use tween.Completed:Wait() and remove the wait(0)

1 Like

Instead of doing that, I’d just advise in doing all of this on the client as well as using lerp instead or something similar like a for loop.

Remember that every time you create a tween, you must destroy it if you aren’t going to use it again as keeping it could lead to very large lag issues (yes, you can destroy it: Tween:Destroy()).

Lastly, even if you’re destroying it while creating it and finishing to wait, thats very hacky and can still probably lead to some lag issues along the lines. A probable better method would actually be a for loop or something similar… like a lerp.

1 Like

I just tried running it on the client, and it seems to be running a lot smoother! The only issue is that when a player is sitting on it, it kinda skips a bit. I’m going to try the lerping now!

1 Like

Here’s a snippet of the new code, its just making the car sit in the middle of the baseplate, which isn’t where it normally is, so that means it is moving.

while true do
local speed = script.Speed.Value -- Time
	

if  part.CFrame == coaster[script.CurrentNumber.Value].CFrame then
	if coaster:FindFirstChild(script.CurrentNumber.Value+1) then
	script.CurrentNumber.Value = script.CurrentNumber.Value+1
	
	end
	local cframe1 = CFrame.new()
	local cframe2 = coaster[script.CurrentNumber.Value].CFrame
	part.CFrame = cframe1:lerp(cframe2, speed)

	elseif not  coaster:FindFirstChild(script.CurrentNumber.Value+1) then
			script.CurrentNumber.Value = 1
	local cframe1 = CFrame.new()
	local cframe2 = coaster[script.CurrentNumber.Value].CFrame
	part.CFrame = cframe1:lerp(cframe2, speed)
		
end
wait(0)
end

You need to do part.CFrame:Lerp()

1 Like

That only made the car go to the cframe of Node 1

Would it be easier if I gave you the place file for you to look at? My discord is happyhal75#2503

You forgot to put the 3rd argument of lerp which is the “percentage” of how far you want it to go, set it to 0.9 for very responsive or 0.1 for smooth/slow, and inbetween values for well inbetween.

1 Like

Where would I put the percentage? I’m a bit confused.