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

Help with Coroutines?

I was trying to make button that stops the weird flickering of the Transparency when a Character touches it with Touched event,but it didn't work out because the Coroutine doesn't start or resume,why? And is there another way to stop the weird flickering of the Transparency? If so please tell me.

local Part = script.Parent
-----------------------------
	ChangeTransparency = coroutine.create(function()--Checks if Transparency is 0.5 if it is then turn it to 0
		if Part.Transparency == 0.5
			then
			Part.Transparency = 0
		end
	end)
	-----------------------------
	coroutine.resume(ChangeTransparency)--Changes the part back to normal just incase it hasn't
	    Part.Touched:connect(function(TouchedPart)    
			Part.Transparency = 0.5
			print("I've been touched")
			coroutine.yield(ChangeTransparency)--Stops coroutine
			end)
			-----------------------------
				Part.TouchEnded:connect(function()
					coroutine.resume(ChangeTransparency)--Changes the part back to normal
				end)
				

1 comment

1

Within the ChangeTransparency function, you'll need to re-define the variables as Lua is basically running it as its own script. An idea for the weird flickering is to use debounce. http://wiki.roblox.com/index.php?title=Debounce#Real_world

1 answer

✓ Accepted answer

Try using debounce;

local Part = script.Parent
local Debounce = false

Part.Touched:connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid")and not Debounce then
Debounce = true
while Debounce == true do
wait(0)
Part.Transparency = 0.5
if Debounce == false then
break
end
end
end
end)

Part.TouchEnded:connect(function()
Debounce = false
Part.Transparency = 0
end)

A bit buggy, but it works. Hope this helped!

1 comment

1

Properly tab your code.

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