Need help with a script!

Hey! I need help with a script. I want to make the background blur when you open the GUI and I’ve achieved that by making it insert the BlurEffect into lighting. However, when you click the button multiple times, it puts multiple blur’s into lighting. This makes it so the close button doesn’t delete the blur. Any ideas on what I could do/change to make them all get deleted?
Open Button:

function click()
	local blur = Instance.new("BlurEffect", game.Lighting)
	script.Disabled = true
	script.Parent.Parent.Main.Visible = true
	script.Parent.Parent.Main:TweenPosition(
		UDim2.new(0.297, 0, 0.215, 0),
		"InOut",
		"Quad",
		1,
		false
	)

	wait(1)
	script.Disabled = false
	script.Parent.Parent.Parent.Visible = true
end
script.Parent.MouseButton1Click:Connect(click)

Close Button:

function click()
	game.Lighting.Blur:Destroy()
	script.Disabled = true
	script.Parent.Parent.Parent:TweenPosition(
		UDim2.new(0.297, 0,-0.5, 0),
		"InOut",
		"Quad",
		1,
		false
	)

	wait(1)
	script.Disabled = false
	script.Parent.Parent.Parent.Visible = false
end
script.Parent.MouseButton1Click:Connect(click)

You could make it so that when you close the buttons, just use for loops to iterate and delete all those blur effect instances. You could also do an if statement to check if a blur instance is already added.

1 Like

In the BlurEffect object, isn’t there a property called Enabled? So when you open the GUI, you can just set the Enabled property to true, and when you close to GUI, set the Enabled property to false. You can default to false in the beginning.

2 Likes

You could set a global boolean to true after you add the blur, and use an if statement to check if this boolean is true or not before adding the blur.

Something like

If not blurAdded then
-- define the blur instance
blurAdded = true
end

The above answer is way better tho :slight_smile:

1 Like

Thanks for your response!
I’m new to scripting. How exactly could I make it loop? I’ve tried and it isnt working so far.
(Sorry for the late reply)

uhm

while true do
	--things u want to loop
end
1 Like

Thanks for the reply, I had already tried that but it wasn’t working. I tried it again but did it differently and it worked!

Add a debounce to prevent spamming the button.

Also you can cleanup the blurs by using the following:

for i, v in pairs(game.Lighting:GetChildren()) do
	if v:IsA("BlurEffect") then
		v:Destroy()
	end
end
1 Like