Improving My Code's Performance

This code was made to, basically, make a part move up and down using TweenService. Its very smooth when I’m playtesting in Studio, but becomes really laggy when I play it on ROBLOX. How do I improve the performance?

local elevator = script.Parent
local TS = game:GetService("TweenService")

while true do
	local goal1 = {}
	goal1.CFrame = elevator.CFrame + Vector3.new(0,2,0)

	local tweenInfo = TweenInfo.new(3, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut) 
	local tween = TS:Create(elevator,tweenInfo,goal1)
	tween:Play()
wait(3)
	local goal2 = {}
	goal2.CFrame = elevator.CFrame - Vector3.new(0,2,0)

	local tweenInfo = TweenInfo.new(3, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut) 
	local tween = TS:Create(elevator,tweenInfo,goal2)
	tween:Play()
wait(3)
end
1 Like

It becomes laggy when playing on a live server because the replication rate from the server to the client is much lower than your frame rate. The only way to solve this would be to tween the part on the client in a local script.

1 Like

A quick question, does having too many local scripts lags the game as well? What I meant is imagine 1 local script, and there’s 100 players in a server and all of them have that one local script. Does it affect the server performance as well or the client will handle the local script instead?

LocalScripts are run by the clients, so I dont think they should affect the server’s performance much, unless you have thousands upon thousands of LocalScripts constantly running (which if you do, you are definitely doing something you aren’t supposed to do)


Also I think you’re supposed to make a new topic if you want to ask a new question