How could i make this code better?

Is there any way i could simplify these tweens? Having like 6 tweens for 2 actions seems excessive! I’m trying to blur/change brightness/change fov when a button is clicked.

local TweenService = game:GetService("TweenService")

	local camera = workspace.CurrentCamera
	local default_Fov = camera.FieldOfView

	-- Tweens
	local tweenInfo = TweenInfo.new(0.4,Enum.EasingStyle.Quint, Enum.EasingDirection.Out)
	local tweenIn = TweenService:Create(camera, tweenInfo, {FieldOfView = 50})
	local tweenOut = TweenService:Create(camera, tweenInfo, {FieldOfView = default_Fov})
	local tweenIn2 = TweenService:Create(game:GetService("Lighting").Blur, tweenInfo, {Size = 11})
	local tweenOut2 = TweenService:Create(game:GetService("Lighting").Blur, tweenInfo, {Size = 0})
	local tweenIn3 = TweenService:Create(game:GetService("Lighting"), tweenInfo, {Brightness = 1})
	local tweenOut3 = TweenService:Create(game:GetService("Lighting"), tweenInfo, {Brightness = 3})

	yes = false
	
	script.Parent.MouseButton1Down:Connect(function()
		if yes == false then
			yes = true
			tweenOut:Destroy()
			tweenOut2:Destroy()
			tweenOut3:Destroy()
			tweenIn:Play()
			tweenIn2:Play()
			tweenIn3:Play()
		else
			yes = false
			tweenIn:Destroy()
			tweenIn2:Destroy()
			tweenIn3:Destroy()
			tweenOut:Play()
			tweenOut2:Play()
			tweenOut3:Play()
		end
	end)

Try putting the tweens in a for loop

In terms of efficiency, that’s pretty much the best way to go about it - Premaking the tweens and playing them when necessary. I don’t think there’s much room to make it cleaner, nor is there any need to since its not a particularly large script.

You can use tables and for-loops to avoid having to run each tween manually. You can also clean up the boolean logic a bit.

local TweenService = game:GetService("TweenService")
local camera = workspace.CurrentCamera
local default_Fov = camera.FieldOfView

-- Tweens
local tweenInfo = TweenInfo.new(0.4,Enum.EasingStyle.Quint, Enum.EasingDirection.Out)
local In = {
    TweenService:Create(camera, tweenInfo, {FieldOfView = 50});
    TweenService:Create(game:GetService("Lighting").Blur, tweenInfo, {Size = 11});
    TweenService:Create(game:GetService("Lighting"), tweenInfo, {Brightness = 1})
}
local Out = {
    TweenService:Create(camera, tweenInfo, {FieldOfView = default_Fov})
    TweenService:Create(game:GetService("Lighting").Blur, tweenInfo, {Size = 0})
    TweenService:Create(game:GetService("Lighting"), tweenInfo, {Brightness = 3})
}

local Direction = false -- Rename variable for better readability

script.Parent.MouseButton1Down:Connect(function()
    if Direction then
        for _,Tween in In do Tween:Destroy() end
        for _,Tween in Out do Tween:Play() end
    else
        for _,Tween in Out do Tween:Destroy() end
        for _,Tween in In do Tween:Play() end
    end
    Direction = not Direction
end)

This doesn’t lower the amount of tweens, as you stil need 2 tweens for each object you are tweening, but this does make it a bit cleaner to read.

This is a lot nicer and easier to work with! Thanks for the feedback, much appreciated.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.