Is this script bad for performance? It’s a server script located inside of StarterCharacterScripts that causes the player to blink every 6-18 seconds. If it is bad for performance, how could I improve it?
Here’s my code:
local TweenService = game:GetService("TweenService")
local Character = script.Parent
local Head = Character:WaitForChild("Head")
local LeftEye = Character:WaitForChild("LeftEye")
local RightEye = Character:WaitForChild("RightEye")
local RandomTime = math.random(6, 18)
local BlinkTime = 0.25
while task.wait(RandomTime) do
LeftEye.Color = Head.Color
RightEye.Color = Head.Color
LeftEye.Material = Head.Material
RightEye.Material = Head.Material
TweenService:Create(LeftEye, TweenInfo.new(BlinkTime), {Size = Vector3.new(0.1, 0.25, 0.55)}):Play()
TweenService:Create(RightEye, TweenInfo.new(BlinkTime), {Size = Vector3.new(0.1, 0.25, 0.55)}):Play()
task.wait(BlinkTime)
LeftEye.Color = Color3.fromRGB(0, 0, 0)
RightEye.Color = Color3.fromRGB(0, 0, 0)
LeftEye.Material = Enum.Material.SmoothPlastic
RightEye.Material = Enum.Material.SmoothPlastic
TweenService:Create(LeftEye, TweenInfo.new(BlinkTime), {Size = Vector3.new(0.6, 0.15, 0.55)}):Play()
TweenService:Create(RightEye, TweenInfo.new(BlinkTime), {Size = Vector3.new(0.6, 0.15, 0.55)}):Play()
end
make each client blink all characters in the game in a localscript this will use less network but the blinks will not be in sync but no one should notice there not in sync
instead of creating new tweens over and over again you could create the tweens once at the top and reuse the same tweens over and over
This isn’t too bad, but it does have some areas worth note.
As @5uphi said, you can reuse the tweens if you set them in a variable before the loop.
And since I’m feeling generous, I did that for you, along with another edit I left comments for.
local TweenService = game:GetService("TweenService")
local Character = script.Parent
local Head = Character:WaitForChild("Head")
local LeftEye = Character:WaitForChild("LeftEye")
local RightEye = Character:WaitForChild("RightEye")
local BlinkTime = 0.25
local BlinkInfo = TweenInfo.new(BlinkTime)
-- Moved these to local variables so they can be reused.
local BlinkLeft1 = TweenService:Create(LeftEye, BlinkInfo, {Size = Vector3.new(0.1, 0.25, 0.55)})
local BlinkRight1 = TweenService:Create(RightEye, BlinkInfo, {Size = Vector3.new(0.1, 0.25, 0.55)})
local BlinkLeft2 = TweenService:Create(LeftEye, BlinkInfo, {Size = Vector3.new(0.6, 0.15, 0.55)})
local BlinkRight2 = TweenService:Create(RightEye, BlinkInfo, {Size = Vector3.new(0.6, 0.15, 0.55)})
while true do -- Unnecessary evaluation could slow you down. Use "while true do" vs "while something() do"
task.wait(math.random(6, 18)) -- Wait inside loop so we're not evaluating the return of task.wait();
LeftEye.Color = Head.Color
RightEye.Color = Head.Color
LeftEye.Material = Head.Material
RightEye.Material = Head.Material
BlinkLeft1:Play()
BlinkRight1:Play()
task.wait(BlinkTime)
LeftEye.Color = Color3.fromRGB(0, 0, 0)
RightEye.Color = Color3.fromRGB(0, 0, 0)
LeftEye.Material = Enum.Material.SmoothPlastic
RightEye.Material = Enum.Material.SmoothPlastic
BlinkLeft2:Play()
BlinkRight2:Play()
end