Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Click a part, part's transparency goes from 0 to 1 slowly?

i've tried a couple of scripts trying to make a block in where when you click it, it's transparency goes from 0 to 0.1 to 0.2 to 0.3 every .2 seconds going all the way to 1 transparency. sorry if i worded that wierd. im pretty sure it has to do with for i, v in pairs statements.

trans = script.Parent.Transparency
function onClicked()
	for i, v in pairs() do
-- wat goes ere?
	end
	
end
script.Parent.ClickDetector.MouseClick:Connect(onClicked)

im kinda new to scripting and wanna learn some more. some help would be nice.

1 comment

0

it has nothing to do with in pair loops

2 answers

✓ Accepted answer

Try this code instead

trans = script.Parent.Transparency

local function OnClicked()
	for i = 1,10,1 do
		trans = i/10
		wait(.2)
	end
end
script.Parent.ClickDetector.MouseClick:Connect(onClicked)

or this


local TweenService = game:GetService("TweenService")

local Part = script.Parent

local Goal = {
	Transparency = 1
}

local tweenInfo = TweenInfo.new(
	2, -- Time
	Enum.EasingStyle.Linear, -- EasingStyle
	Enum.EasingDirection.Out, -- EasingDirection
	0, -- RepeatCount (when less than zero the tween will loop indefinitely)
	false, -- Reverses (tween will reverse once reaching it's goal)
	0 -- DelayTime
)

local Tween = TweenService:Create(Part, tweenInfo, Goal)

local function OnClicked()
	Tween:Play()
end

script.Parent.ClickDetector.MouseClick:Connect(onClicked)

Using this one is more efficient since you can do more than just change the transparency. You could also change it's BrickColor

local TweenService = game:GetService("TweenService")

local Part = script.Parent

local Goal = {
	Transparency = 1,
	BrickColor = BrickColor.new("Really red")
}

local tweenInfo = TweenInfo.new(
	2, -- Time
	Enum.EasingStyle.Linear, -- EasingStyle
	Enum.EasingDirection.Out, -- EasingDirection
	0, -- RepeatCount (when less than zero the tween will loop indefinitely)
	false, -- Reverses (tween will reverse once reaching it's goal)
	0 -- DelayTime
)

local Tween = TweenService:Create(Part, tweenInfo, Goal)

local function OnClicked()
	Tween:Play()
end

script.Parent.ClickDetector.MouseClick:Connect(onClicked)

4 comments

0

Don't use BrickColor I made an error

0

thanks so much, i really needed this.

0

btw for some reason the first one doesn;t work, dunno why

0

What was the error with the first code I’ll fix it

Alright so first things first, It has nothing to do with in pairs loops, You just use repeat which repeats code that you put until whenever you want it to stop. In this case we need to make the transparency go up to 1 so we will use repeat until

repeat

until

And use wait(2) to wait 2 seconds then higher the transparency by 0.1 until the transparency is 1 like this

local trans = script.Parent.Transparency

function onClicked()
repeat
wait(2)
trans = trans + 0.1
until trans == 1
end
end

script.Parent.ClickDetector.MouseClick:Connect(function(onClicked)

This discussion is preserved as it was. New answers, comments, and votes are closed.