Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I simplify this script?

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

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)

2 comments

0

Thanks :3, just one question. What is fading = false?

0

Fading is the debounce that the script uses to tell if it is already in action or not.

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

3 comments

0

Thanks :) Never knew what a Debounce was till now

0

No problem! Make sure to accept an answer if it works.

0

I worked but I had to change it up a bit to add in a CanCollide=False at Transparency=0

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.