Script doesn't activate BackgroundTransparency -= i

local player = game.Players.LocalPlayer

script.Parent.MouseButton1Down:Connect(function()
	for i = 1,0,0.1 do
		player:WaitForChild("PlayerGui"):WaitForChild("blackscreen").Frame.BackgroundTransparency -= i
		task.wait(0.02)
		end
end)

Hello , My script doesn’t really work well and I don’t know the issue of it not working. Does somebody knows the bug ?

1 Like

Since you want that to decrease , you need to change 0.1 to -0.1
local player = game.Players.LocalPlayer

script.Parent.MouseButton1Down:Connect(function()
	for i = 1,0,-0.1 do
		script.Parent.Parent.Frame.BackgroundTransparency = 1- i
		task.wait(0.02)
	end
end)
2 Likes

What do you mean? Could you please explain what line.

1 Like

1- When you’re trying to reduce in a for loop, you’ll need to put the 3rd option as a negative number.

2-Because you wanted the transparency to fade away accordingly, you had to put it as 1-i,
so by that, it’d fade away like it.

1 Like

It is very fast tho. I mean when you click the button , it fade very fast.

You could change the decrement to something lower

Increase task.wait(0.02) to some longer yield.

You can do this without subtracting from the background transparency and just set it to i directly

And because its subtracting the transparency from an ever changing number, the transparency won’t exactly be 1. It would more so be something like this
Iteration 1:
BackgroundTransparency = 0.9
i = 0.1

Iteration 2:
BackgroundTransparency = 0.7
i = 0.2

Iteration 3:
BackgroundTransparency = 0.4
i = 0.3

Iteration 4:
BackgroundTransparency = 0.1
i = 0.4
etc

local player = game.Players.LocalPlayer

script.Parent.MouseButton1Down:Connect(function()
	for i = 0,1,0.1 do -- counts down instead of up because it starts at 0 and counts to 1 with increments of 0.1
		player:WaitForChild("PlayerGui"):WaitForChild("blackscreen").Frame.BackgroundTransparency = i
		task.wait(0.02)
		end
end)

Alternatively you could instead use TweenService for a smoother effect (and one that won’t stop the script because of a loop).

local player = game.Players.LocalPlayer

script.Parent.MouseButton1Down:Connect(function()
	local goal = {}
	goal.BackgroundTransparency = 1

	local duration = 1
	local easing_style = Enum.EasingStyle.Linear
	local easying_direction = Enum.EasingDirection.In
	game.TweenService:Create(player:WaitForChild("PlayerGui"):WaitForChild("blackscreen").Frame,TweenInfo.new(duration,easing_style,easying_direction),goal):Play()
end)

He could either do that or decrease the 3rd value in the for loop

I’d personally recommend using the TweenService as the previously reply did.

True, TweenService is smoother and better, I guess the post’s owner wants to use the classic way.