Making a blur fade more efficient?

Hi developers,

I have made a simple script to fade away a Blur, but I would like feedback on making this more efficient.

Script:

local function fadeOut()
	game.Lighting.PremiumMenu.Enabled = true
	game.Lighting.PremiumMenu.Size = 50
	wait()
	game.Lighting.PremiumMenu.Size = 45
	wait(0.025)
	game.Lighting.PremiumMenu.Size = 40
	wait(0.025)
	game.Lighting.PremiumMenu.Size = 35
	wait(0.025)
	game.Lighting.PremiumMenu.Size = 30
	wait(0.025)
	game.Lighting.PremiumMenu.Size = 25
	wait(0.025)
	game.Lighting.PremiumMenu.Size = 20
	wait(0.025)
	game.Lighting.PremiumMenu.Size = 15
	wait(0.025)
	game.Lighting.PremiumMenu.Size = 10
	wait(0.025)
	game.Lighting.PremiumMenu.Size = 5
	wait(0.025)
	game.Lighting.PremiumMenu.Size = 0
end

(PremiumMenu is a BlurObject in Lighting)

2 Likes

You could utilize a loop to make it more of an “automatic” process rather than needing to copy paste those lines of code and change the value each time.

Additionally, the lowest amount of time that you can use for “wait” is 0.03 seconds if I remember correctly [ which is achievable by just saying wait() ], so there’s no need to add wait(0.025).

Here’s an example code block utilizing a loop:

local Lighting = game:GetService("Lighting")

local function fadeOut()
    local PremiumMenu = Lighting.PremiumMenu
    PremiumMenu.Enabled = true

--[[
"50" is the starting number
"0" is the end number
"-5" is the increment

The loop below will make the effect start at 50 size, end at 0, and it'll
lower the size in increments of "-5" each time
--]]
    for CurrentBlurSize = 50, 0, -5 do
        wait()
        PremiumMenu.Size = CurrentBlurSize
    end
end

More information about loops can be found on this Developer Hub Article.

2 Likes

You could always use the TweenService! TweenService can be used to tween most properties, and gives you a lot of easing styles and directions.

(not sure why it was replied to @StrongBigeMan9 ^^;)

1 Like

I would recommend switching over to TweenService as it’d make the blur fade out more smoothly.
I also stored Lighting and PremiumMenuBlur in variables to reduce repetition.
With these changes made, here’s how the script would look.
(the 0.25 in the TweenInfo is how long the tween lasts)

local Lighting = game:GetService("Lighting")
local TweenService = game:GetService("TweenService")

local PremiumMenuBlur = Lighting.PremiumMenuBlur

local FadeOutTweenInfo = TweenInfo.new(
	0.25,
	Enum.EasingStyle.Linear
)
local FadeOutTween = TweenService:Create(PremiumMenuBlur, FadeOutTweenInfo, {Size = 0})

local function FadeOut()
	PremiumMenuBlur.Enabled = true
	PremiumMenuBlur.Size = 50
	FadeOutTween:Play()
end

edit changed spaces to tabs
edit 2 fixed comma error, thanks dukzae

1 Like

Just curious - How would I make it fade in?

1 Like

You can change the {Size = 0} to {Size = 50}.

Okay, I will try this out now. Thanks for helping!

1 Like

You’d have to change the starting size to 0 and the end size to be 50.

local FadeInTween = TweenService:Create(PremiumMenuBlur, FadeOutTweenInfo, {Size = 50})

local function FadeIn()
	PremiumMenuBlur.Enabled = true
	PremiumMenuBlur.Size = 0
	FadeInTween:Play()
end

Oh, okay. Updating the script now to include these lines.

Didn’t work, I got the error:

Players.dude8074.PlayerGui.PremiumGui.UserWithPremium.TweenScript:12: Expected identifier when parsing expression, got ')'

It seems to be a single bracket causing the error:

Screen Shot 2021-03-02 at 6.32.08 PM

Can you show us the whole code? It would be much more helpful.

Sure! This is a script for opening/closing a GUI, and the blur fades in/out when you open/close the GUI.

local START_POSITION = UDim2.new(-0.5, 0,2, 0)
local GOAL_POSITION = UDim2.new(-0.5,0,-0.5,0)

local guiObject = script.Parent

local Lighting = game:GetService("Lighting")
local TweenService = game:GetService("TweenService")
local PremiumMenuBlur = Lighting:WaitForChild("PremiumMenuBlur")

local FadeOutTweenInfo = TweenInfo.new(
	0.25,
	Enum.EasingStyle.Linear,
)

local function fadeOut()
	local FadeOutTween = TweenService:Create(PremiumMenuBlur, FadeOutTweenInfo, {Size = 0})
	PremiumMenuBlur.Enabled = true
	PremiumMenuBlur.Size = 50
	FadeOutTween:Play()
end

local function fadeIn()
	local FadeInTween = TweenService:Create(PremiumMenuBlur, FadeOutTweenInfo, {Size = 50})
	PremiumMenuBlur.Enabled = true
	PremiumMenuBlur.Size = 0
	FadeInTween:Play()
end


local function callback(state)
	if state == Enum.TweenStatus.Completed then
		-- Yay
	elseif state == Enum.TweenStatus.Canceled then
		-- :(
	end
end

-- Initialize the GuiObject position, then start the tween:
guiObject.Position = START_POSITION
script.Parent.Parent.TopBarModifier.RightFrame.openWithPremium.MouseButton1Click:Connect(function()
	if guiObject.Position == START_POSITION then
		fadeIn()
		local willPlay = guiObject:TweenPosition(
			GOAL_POSITION,           -- Final position the tween should reach
			Enum.EasingDirection.Out, -- Direction of the easing
			Enum.EasingStyle.Sine,   -- Kind of easing to apply
			0.75,                       -- Duration of the tween in seconds
			true,                    -- Whether in-progress tweens are interrupted
			callback                 -- Function to be callled when on completion/cancelation
		)
		if willPlay then
			-- It will play...
		else
			-- No play :(
		end
	else
		local willPlay = guiObject:TweenPosition(
			START_POSITION,           -- Final position the tween should reach
			Enum.EasingDirection.In, -- Direction of the easing
			Enum.EasingStyle.Sine,   -- Kind of easing to apply
			0.75,                       -- Duration of the tween in seconds
			true,                    -- Whether in-progress tweens are interrupted
			callback                 -- Function to be callled when on completion/cancelation
		)
		wait(0.75)
		fadeOut()
		if willPlay then
			-- It will play...
		else
			-- No play :(
		end
	end
end)

FYI, I am using WaitForChild on the blur because I have a script insert it to Lighting.

Ah, I see the problem. You have a comma at the end of

Enum.EasingStyle.Linear

at line 12. It thinks that you are gonna put the next enum, but since you’re not, you could just remove the comma.

1 Like

Okay, it works perfectly! Thank you all so much for your help! :')

3 Likes

Sorry about that, I accidentally left the comma there when I was writing the script. I’ll edit my solution so anyone who may find this topic in the future and use that code doesn’t run into an error.

Extremely short way:

local premiumMenu = game:GetService("Lightning").PremiumMenu
premiumMenu.Enabled = true

for blurSize = 50, 0, -5 do
    premiumMenu.Size = blurSize
    wait(0.025)
end
1 Like