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

How do I make this brick fading faster and efficient?

Asked by 8 years ago

I wrote a code so a brick will fall, fade and disappear. However, is there a more efficient way to code the fade rather than having to write it out over and over again?

CODE:

local partParent = script.Parent

function onTouch(part)
    local human = part.Parent:FindFirstChild("Humanoid")
    if (human ~= nil) then
        script.Parent.Anchored = false
        wait(5)
        partParent.Transparency = .1
        wait(.01)
        partParent.Transparency = .2
        wait(.01)
        partParent.Transparency = .3
        wait(.01)
        partParent.Transparency = .4
        wait(.01)
        partParent.Transparency = .5
        wait(.01)
        partParent.Transparency = .6
        wait(.01)
        partParent.Transparency = .7
        wait(.01)
        partParent.Transparency = .8
        wait(.01)
        partParent.Transparency = .9
        wait(.01)
        partParent.Transparency = 1
        wait(.01)
        game.Workspace.fallingPart:Destroy()
    end
end

partParent.Touched:connect(onTouch)

2 answers

Log in to vote
5
Answered by 8 years ago

For Loops!

What is a for loop?

In Lua, you would write a for loop like this,

for i = 1,10 do
    print(i)
end

The output would look like this,

1
2
3
4
5
6
7
8
9
10

i is a variable that will get added by 1 each time. Normally you would add a number to i each time, but Lua will automatically do this for you. If you wanted to make i count down, you would subtract by 1 each time, like so,

for i = 10,1,-1 do
    print(i)
end
Counts down from 10

Your question

To make your code shorter, just do a for loop, like so,

local partParent = script.Parent

function onTouch(part)
    local human = part.Parent:FindFirstChild("Humanoid")
    if (human ~= nil) then
        partParent.Anchored = false
        wait(5)
        for i = 1, 10 do
            partParent.Transparency = i/10
            wait(.01)
        end
        game.Workspace.fallingPart:Destroy()
    end
end

partParent.Touched:connect(onTouch)

Optional changes

You could also combine your event and function like this,

local partParent = script.Parent

partParent.Touched:connect(function(part)
    local human = part.Parent:FindFirstChild("Humanoid")
    if (human ~= nil) then
        partParent.Anchored = false
        wait(5)
        for i = 1, 10 do
            partParent.Transparency = i/10
            wait(.01)
        end
        game.Workspace.fallingPart:Destroy()
    end
end)

You could even get rid of the variable as it's only making the script larger. This does nothing but shorten the script though,

script.Parent.Touched:connect(function(part)
    local human = part.Parent:FindFirstChild("Humanoid")
    if (human ~= nil) then
        script.Parent.Anchored = false
        wait(5)
        for i = 1, 10 do
            script.Parent.Transparency = i/10
            wait(.01)
        end
        game.Workspace.fallingPart:Destroy()
    end
end)

Last optional change would be removing the human variable because you're not using it.

script.Parent.Touched:connect(function(part)
    if part.Parent:FindFirstChild("Humanoid") then
        script.Parent.Anchored = false
        wait(5)
        for i = 1, 10 do
            script.Parent.Transparency = i/10
            wait(.01)
        end
        game.Workspace.fallingPart:Destroy()
    end
end)

Side note,

Your script will now be shorter, but this does almost nothing to affect it's efficiency. There's really not much you can do that would change the efficiency of your script. If I were you, I wouldn't worry about efficiency so much. You should however work on making your scripts smaller. Less code is easier to handle.

Hope I helped!

Good Luck!

1
Oh wow, this is cool! I just rewrote a script from the Hour of Code tutrial to do this. Thanks! XxDarkMiragexX 45 — 8y
0
np User#11440 120 — 8y
Ad
Log in to vote
2
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
8 years ago

What you need

You need to take advantage of the numerical 'for' loop. This takes in a starting point, ending point, and iterates through the numbers by the given increment, or 0 if none is given.

The syntax looks like this: for variable = start, end, increment do

So have your starting point as 0, the ending point as 1, and the increment as .1


Code

local partParent = script.Parent

function onTouch(part)
    local human = part.Parent:FindFirstChild("Humanoid")
    if (human ~= nil) then
        script.Parent.Anchored = false
        wait(5)
        for i = 0,1,.1 do
            partParent.Transparency = i
        end
        game.Workspace.fallingPart:Destroy()
    end
end

partParent.Touched:connect(onTouch)
2
Since I couldn't accept both answers, I just up voted your answer. Thanks! XxDarkMiragexX 45 — 8y

Answer this question