Hello there,
I can see many improvements in your script that could help reducing the lag
The improvements you can make:
local Players = game:GetService("Players")
local workSpace = game:GetService("Workspace")
local ts = game:WaitForChild("TweenService")
local OpenLazer = false
local ti = TweenInfo.new(4, Enum.EasingStyle.Sine, Enum.EasingDirection.In, -1)
--The -1 in the TweenInfo.new() is the repeat count, -1 means infinite.
--This is better because you only need to call :Play() once
local Lazer1 = workSpace:WaitForChild("killBrick").Lazer1
local Lazer2 = workSpace:WaitForChild("killBrick").Lazer2
local Lazer3 = workSpace:WaitForChild("killBrick").Lazer3
--If the script is a localscript, then you dont need the :WaitForChild() because the parts are loaded together with the localscript
--Predefine the tween so you dont have to make one everytime LazerTween() is called
local function LazerTween()
--You can remove the while true loop because the tween will loop forever
while true do
ts:Create(Lazer1, ti, {Position = Lazer1.Position + Vector3.new(0, 0, 35)}):Play()
ts:Create(Lazer2, ti, {Position = Lazer2.Position + Vector3.new(0, 0, 38)}):Play()
ts:Create(Lazer3, ti, {Position = Lazer3.Position + Vector3.new(0, 0, 10)}):Play()
wait(4)
ts:Create(Lazer1, ti, {Position = Lazer1.Position + Vector3.new(0, 0, -35)}):Play()
ts:Create(Lazer2, ti, {Position = Lazer2.Position + Vector3.new(0, 0, -38)}):Play()
ts:Create(Lazer3, ti, {Position = Lazer3.Position + Vector3.new(0, 0, -10)}):Play()
wait(4)
end
end
game.Players.PlayerAdded:Connect(function()
if not OpenLazer and #Players:GetChildren() >= 1 then
OpenLazer = true
LazerTween()
end
end)
The script with the improvements:
local Players = game:GetService("Players")
local workSpace = game:GetService("Workspace")
local ts = game:WaitForChild("TweenService")
local OpenLazer = false
local ti = TweenInfo.new(4, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, -1)
local Lazer1 = workSpace:WaitForChild("killBrick").Lazer1
local Lazer2 = workSpace:WaitForChild("killBrick").Lazer2
local Lazer3 = workSpace:WaitForChild("killBrick").Lazer3
--I changed the position of the lazers so they would start from the beginning
Lazer1.Position = Lazer1.Position + Vector3.new(0, 0, -35)
Lazer2.Position = Lazer2.Position + Vector3.new(0, 0, -38)
Lazer3.Position = Lazer3.Position + Vector3.new(0, 0, -10)
Tween1 = ts:Create(Lazer1, ti, {Position = Lazer1.Position + Vector3.new(0, 0, 70)})
Tween2 = ts:Create(Lazer2, ti, {Position = Lazer2.Position + Vector3.new(0, 0, 76)})
Tween3 = ts:Create(Lazer3, ti, {Position = Lazer3.Position + Vector3.new(0, 0, 20)})
local function LazerTween()
Tween1:Play()
Tween2:Play()
Tween3:Play()
end
game.Players.PlayerAdded:Connect(function()
if not OpenLazer and #Players:GetChildren() >= 1 then
OpenLazer = true
LazerTween()
end
end)