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)
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
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)
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)
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!
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
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)