Script causing extreme lag

Hey! :wave:

I’m experiencing extremely severe lag when trying to join the game I’m developing to test it on Roblox Studio, and I’ve finally found the script that’s causing the lag after disabling many of them. The script is provided below; the lag that it causes is so severe that it makes my Roblox Studio crash. Let me know if anyone knows why the script is causing this issue and if there’s any way of fixing it; any and all help is appreciated. Thank you. :heart:

local Gui = script.Parent
local tweenInfo = TweenInfo.new(5)
local h = true
local tween1 =  game:GetService("TweenService"):Create(Gui, tweenInfo,{Rotation = (-5)})
local tween2 =  game:GetService("TweenService"):Create(Gui, tweenInfo,{Rotation = (5)})

while h == true do
	tween1:Play()
	tween1.Completed:Connect(function()
		tween2:Play()
	end)     
	tween2.Completed:Connect(function()
		tween1:Play()
	end)
end
2 Likes

You forgot to include a wait(). Currently your code is running infinitely fast. To fix this, just do:

local TweenService = game:GetService("TweenService")

local Gui = script.Parent
local tweenInfo = TweenInfo.new(5)
local tween1 = TweenService:Create(Gui, tweenInfo, {Rotation = (-5)})
local tween2 = TweenService:Create(Gui, tweenInfo, {Rotation = (5)})

local active = true

while task.wait() and active do
	tween1:Play()
	tween1.Completed:Wait()
	tween2:Play()
	tween2.Completed:Wait()
end

Also I fixed up your code logic.

2 Likes