Lag Optimization Tips Needed!

Oh and the train has a few hundred parts

Helpful tip. Thanks, I will try that as well. Still setting it up…

Are you moving these objects inside a LocalScript? Because if you are changing its positions inside a Server script, it will force a data transmission between the server and the client, causing a delay.

1 Like

CFrames do not cause any significant lag at all, it is really efficient

1 Like

It causes, I already used that to movement and it is really unefficient.

Sorry, but you are wrong. The CFrame calculation has minimal processing needs. In addition, CFrame is essential if you want to make more complex movements, such as using ToWorldSpace.
But, not to remain in speculation, make a benchmark, testing the difference in processing time between changing a CFrame and comparing the time of changing Position and Orientation and draw your conclusions.
If you want an idea of a benchmark script for this, use this:

Furthermore, I reinforce that the lag mentioned by the OP may have more to do with changing the CFrame within the Server and not within the LocalScript.

I know but for a loop it doesn’t turn it the more efficient thing to use.

Again, make the benchmark I suggested above and prove it to yourself.

Well… because of the arguments… I have no idea what to put in my script now. This is what I have currently…

-------Tween test

local TweenService = game:GetService(“TweenService”)

local TweenPart = Instance.new(“Part”, workspace)

TweenPart.Name = “TweenagePart”

TweenPart.Anchored = true

TweenPart.Color = Color3.fromRGB(255, 255, 127)

TweenPart.Size = Vector3.new(4, 4, 4)

TweenPart.Position = Vector3.new(0, 0, 10)

Tweens = TweenInfo.new(

1, – How long it takes to complete.

Enum.EasingStyle.Linear, – The way the part moves. (Animated)

Enum.EasingDirection.InOut, – What direction it moves in.

-1, – How many times it loops. (Number less than 0 = Inf Loop.)

true,

0.6

)

local Animator = TweenService:Create(TweenPart, Tweens, {Position = TweenPart.Position+Vector3.new(20, 0, 0)})

Animator:Play()

task.wait(5)

Please don’t argue. That takes me FURTHER away from what I need to accomplish. If you can just give me anti-lag tips… I’d appreciate that a lot. I don’t know what to use right now.

Also no. They’re in a normal script, or global script.

Thanks. Tween service works and removed the lag slightly. Still unfortunately having some issues though.

So do it inside a localscript and compare the difference

This is not true

This is also not true, using wait() does not cause lag. Although I still suggest using RunService.Heartbeat, or switching to task.wait().

wait() is now deprecated in favor of task.wait()

Tween service is the same thing, just has more customization. It’s still changing the CFrame rappidly though.


Moving that amount of parts shouldn’t be causing the lag unless it’s huge amounts of them.

I suggest looking through other scripts you may have in your game and providing more details.

If you are moving a large amount of parts, then I suggest using a local script and only moving parts that are within a certain distance to the camera.

You can always use tweening, but if you do, I suggest using it on a local script so the server doesnt have to replicate it.


Feel free to ask if you need help with any of these

Okay so, I notice that the heartbeat movement is recommended. However… how do I make it move using RunService? CFrame? Position?

Here’s what it may look like:

-- On a local script. You will have to modify this since its psuedo code

local RunService = game:GetService("RunService")
local PartsToMove = workspace.Parts:GetChildren() -- How ever you want to get the table of parts

RunService.Heartbeat:Connect(function()
	
	-- This will go off every heartbeat
	
	for _, Part in pairs( PartsToMove ) do -- Get all the parts in the PartsToMove table
		
		-- You could always check the parts distance from the camera so you only move parts close to the camera
		
		Part.CFrame = -- How ever you will modify the cframe
		
	end
	
end)

Thank, I will remember that. :smiley_cat:

Okay so… with the tweening… I tried it and… it worked. The game is playable now… but the game still lags kind of badly… so I’m going to try run service now

Also… why is the part not moving straight? It’s literally moving to the right, when it’s supposed to go straight. The axis is Z, not X. So not sure why it’s moving like that. (This is with the tweening)

body velocity is Deprecated, I think its linear velocity, I’m not sure or angular velocity.