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

How do I simplify this script?

Asked by
Scerzy 85
10 years ago
brick = game.Workspace.Brick

function Spleef()
brick.Transparency = 0.1
wait(.1)
brick.Transparency = 0.2
wait(.1)
brick.Transparency = 0.3
wait(.1)
brick.Transparency = 0.4
wait(.1)
brick.Transparency = 0.5
wait(.1)
brick.Transparency = 0.6
wait(.1)
brick.Transparency = 0.7
wait(.1)
brick.Transparency = 0.8
wait(.1)
brick.Transparency = 0.9
wait(.1)
brick.Transparency = 1.0
wait(2)
brick.Transparency = 0

end

brick.Touched:connect(Spleef)

The problem with this script is that if you jump on it it will gradually fade, but if you continuously walk on the brick, you'd keep resetting the script so the brick's transparency would go back to 0 and then restart the function all over again. Is there any way tp simplify the script so that multiple touches will still allow the brick to gradually fade away?

3 answers

Log in to vote
0
Answered by
Marolex 45
10 years ago

to prevent the event from reoccurring use a debounce. And you can make it more efficient by using a loop.

fading = false
game.Workspace.Brick.Touched:connect(function(part)
    if not fading then
        fading = true
        for i = 1, 10 do
            game.Workspace.Brick.Transparency = game.Workspace.Brick.Transparency + .1
            wait(.1)
        end
        wait(2)
        game.Workspace.Brick.Transparency = 0
        fading = false
    end
end)
0
Thanks :3, just one question. What is fading = false? Scerzy 85 — 10y
0
Fading is the debounce that the script uses to tell if it is already in action or not. Marolex 45 — 10y
Ad
Log in to vote
0
Answered by
TofuBytes 500 Moderation Voter
10 years ago

Well, you were missing the debounce on your script. I also shortened the line of code, so it loops for transparency by .1 until it gets to 1.

brick = game.Workspace.Brick
local Touched = false --the debounce.

function Spleef()
if not Touched then --if the debounce is false
Touched = true --we turn it true so it doesn't work when someone steps on it.
for melt=0,1,.1 do --we loop it from 0-1 by .1 seconds.
brick.Transparency = (melt)
wait(.1)
end
wait(2)
brick.Transparency = 0
Touched = false --turn the debounce back to false so it works again.
end
end

brick.Touched:connect(Spleef)

Hope this helps you! Good luck! :)

0
Thanks :) Never knew what a Debounce was till now Scerzy 85 — 10y
0
No problem! Make sure to accept an answer if it works. TofuBytes 500 — 10y
0
I worked but I had to change it up a bit to add in a CanCollide=False at Transparency=0 Scerzy 85 — 10y
Log in to vote
0
Answered by
Vividex 162
10 years ago

Please don't double post the same question... but:

brick = game.Workspace.Brick --Simple variable

function Spleef() --Labels the function
    for i = .1, 1, 0.1 do --Does a basic for loop, saying that it stars off by going to 0.1 transparency to 1 while waiting 0.1 seconds each time
    brick.Transparency = i --Gives the transparency affect to the brick
end --Ends the function

script.Parent.Touched:connect(Spleef) --Labels how the function will work (When someone touches it)

Answer this question