Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

How to make part's transparency fade to 1?

Asked by 5 years ago

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

Log in to vote
1
Answered by
clc02 553 Moderation Voter
5 years ago

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.

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 clc02 553 — 5y
0
If I remove the condition on line 08, would I have to remove the "end" on line 15 also? Kaexotix 57 — 5y
0
The comment below is correct* clc02 553 — 5y
0
Or is the condition on line 08's end at line 10? Kaexotix 57 — 5y
View all comments (2 more)
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. Kaexotix 57 — 5y
0
`:connect is depreciated` hypocrite; you made an entire forum thread about deprecation. Btw, it's deprecated, not depreciated. TheeDeathCaster 2368 — 5y
Ad
Log in to vote
0
Answered by
Thetacah 712 Moderation Voter
5 years ago
Edited 5 years ago

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
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 theking48989987 2147 — 5y

Answer this question