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

Why isn't my brick disappearing?

Asked by 10 years ago

Hello, I am a beginner at coding.

I tried to code a brick so when it would be touched, it would slowly become transparent, and allow players to fall through. It didn't work.

Please fix the coding error in this, thanks!

print 'Spleef Brick'
script.Parent.Touched:connect(function(t)
    Workspace.Parent.Transparency = .1
    wait (.2)
    Workspace.Parent.Transparency = .2
    wait (.2)
    Workspace.Parent.Transparency = .3
    wait (.2)
    Workspace.Parent.Transparency = .4
    wait (.2)
    Workspace.Parent.Transparency = .5
    wait (.2)
    Workspace.Parent.Transparency = .6
    wait (.2)
    Workspace.Parent.Transparency = .7
    wait (.2)
    Workspace.Parent.Transparency = .8
    wait (.2)
    Workspace.Parent.Transparency = .9
    wait (.2)
    Workspace.Parent.Transparency = 1
    Workspace.Parent.CanCollide = False
    wait (2)
    Workspace.Parent.Transparency = .9
    Workspace.Parent.CanCollide = True
    wait (.2)
    Workspace.Parent.Transparency = .8
    wait (.2)
    Workspace.Parent.Transparency = .7
    wait (.2)
    Workspace.Parent.Transparency = .6
    wait (.2)
    Workspace.Parent.Transparency = .5
    wait (.2)
    Workspace.Parent.Transparency = .4
    wait (.2)
    Workspace.Parent.Transparency = .3
    wait (.2)
    Workspace.Parent.Transparency = .2
    wait (.2)
    Workspace.Parent.Transparency = .1
    wait (.2)
end)

3 answers

Log in to vote
3
Answered by
Sparker22 190
10 years ago

The problem is that you are doing

Workspace.Parent

When it really should be

script.Parent

Workspace.Parent references to the actual place as a whole. script.Parent references to the script's parent which if the script is a child of the object you are trying to manipulate, script.Parent would be the correct path.

Also, you might want to look into for loops as it makes it far easier to do transparency. You could do

script.Parent.Touched:connect(function(t)
    for i  = 0, 1, 0.1 do
        wait(.2)
        script.Parent.Transparency = i
    end
    script.Parent.CanCollide = false

    for i  = 1,0, -0.1 do
        wait(.2)
        script.Parent.Transparency = i
    end
    script.Parent.CanCollide = true
end)
0
Thank you so much! MacNCheeseGuy 0 — 10y
Ad
Log in to vote
0
Answered by 10 years ago

Your problem is that you are attempting to access Workspace's parent, therefore you are trying to access the Game.

Instead of that, you would need to access the script's parent.

Also, there is a much more efficient way of changing the transparency, such as using a for loop or repeat loop.

Here is the corrected code, explained in comments:

print("Spleef brick")
brick = script.Parent
increment = 0.1
script.Parent.Touched:connect(function(t)

    --this repeat loop repeats all code in between the "repeat" and the "until" until the condition stated after the "until" statement. Therefore, this will repeat adding the Brick's transparency by 0.1 until the Transparency is equal to 1, or completely transparent.

    repeat
        brick.Transparency = Brick.Transparency + increment
        wait(.2)
    until
        brick.Transparency == 1

        brick.CanCollide = false
        wait(2)
        brick.CanCollide = true

    --this is the same as the above loop, except it makes it less transparent rather than more.
    repeat
        brick.Transparency = Brick.Transparency - increment
        wait(.2)
    until
        brick.Transparency == 0


end)

Now, there are other ways to do this, for example, a for loop. I just wanted to use repeat in this instance, as it is easier for beginners to understand.

Hope I helped!

Log in to vote
0
Answered by 10 years ago

Workspace.parent is your problem if you didn't know already by previous comments.

Answer this question