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

How to make part's transparency fade to 1?

In my game, when a player steps onto a brick, the brick is supposed to fade away and once completely transparent, turn CanCollide to false. I tried this script, but it did not do anything when the player steps on the brick.

local trans = script.Parent.Transparency
function onTouch(hit)
	while trans < 1 do
		trans = 0.1
		wait(0.1)
		trans = trans + 0.1
	
	if trans == 1 then
		script.Parent.CanCollide = false
	end
	wait(3)
	trans = 0
	script.Parent.CanCollide = true 
        end
end

script.Parent.Touched:connect(onTouch)

Please help.

2 answers

✓ Accepted answer

Your < is lonely on line 3, as transparency is capped at 1, using <= is probably what you want to do.

Additionally, remove the conditional on line 08, and move lines 9 and 11-13 to after line 14. Once the while loop breaks it's already guaranteed to be completely transparent.

Lastly trans is interpreted as a value, not an object, you'll need to use something like part = script.Parent // part.Transparency = part.Transparency + 0.1 instead of using trans = trans + 0.1, that goes for the while condition as well.

6 comments

0

Oh and before I forget, :connect is depreciated for some unknown reason, please use :Connect which does the same exact thing, as there's a possibility some time eventually :connect may do something different

0

If I remove the condition on line 08, would I have to remove the "end" on line 15 also?

0

The comment below is correct*

0

Or is the condition on line 08's end at line 10?

0

So I ran the code, and just in case I turned on Debug Errors "On All Exceptions". When I stepped on the brick, the game stopped and brought me to the script on line 03. The error read: "attempt to index upvalue 'part' (a number value)". If you would like I could show you the script I used, in case it contains an error.

0

:connect is depreciated hypocrite; you made an entire forum thread about deprecation. Btw, it's deprecated, not depreciated.

I tried working with your code but I kept encountering odd behaviour, so I optimized it a bit. The code below is there if needed, but I recommend not using it since it doesn't exactly help you per say (Its more of my own created script than an edit of yours).

Either way, its up to you.

local enabled = true --a debounce variable
local part = script.Parent -- This is the part we're dealing with

function onTouched()
	if not enabled then return end --If enabled==false, stop the function. It is currently operating transparency changes
	enabled = false --Make enabled = false so we won't encounter any issues when the part is touched multiple times
	for i = 1, 10 do --while loops are recommended for these type of problems. This loop will run 10 times. i = the amount of times the loop has ran
		part.Transparency = i/10 --Amount of time the loop ran divided by 10 gives a 0.1 decrease per run.
		wait(1)--Wait one second before repeating the code.
	end
	part.CanCollide = false --The loop is now done, and the tranparency should be 10/10 (1). We set the parts cancollide to false
	wait(3)--Wait three seconds
	part.CanCollide = true --Make the part CanCollide=true again
	part.Transparency = 0 --Make the part visible again
	enabled = true --Reenable it.
end


script.Parent.Touched:Connect(onTouched) -- Connection line

1 comment

0

please explain your code outside of your scripts, as that is more visually appealing than a script crowded with a lot of long comments

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