Help changing script and adding coroutine

Im trying to make the glass parts change altogether, and I want to add in coroutines since its recommended.

Currently, what happens is this:
robloxapp-20240327-1533193.wmv (302.2 KB)

I’ve tried to change it up a bit to see if it would allow them to both work at the same time, but ended up not working as I had hoped.

my code:

local Glass = workspace.SGGlassWindows:GetChildren()
local Turn = true
function Click()
   for _,x in pairs(Glass) do
   	if x.Name == "Glass" and x:IsA("Part") then
   		if Turn == true then
   			x.Transparency= 0.65 
   			wait()               
   			x.Transparency= 0.1
   			wait()
   			x.Transparency= 0
   		else
   			x.Transparency= 0 
   			wait()               
   			x.Transparency= 0.1
   			wait()
   			x.Transparency= 0.65
   		end
   	end
   end

   if Turn == true then
   	Turn = false
   	script.Parent.Color = Color3.fromRGB(105,255,105)
   	wait(0.1)               
   	script.Parent.Color = Color3.fromRGB(132, 140, 55)
   	wait(0.1)
   	script.Parent.Color = Color3.fromRGB(140, 54, 54)
   else
   	Turn = true
   	script.Parent.Color = Color3.fromRGB(140, 54, 54)
   	wait(0.1)               
   	script.Parent.Color = Color3.fromRGB(132, 140, 55)
   	wait(0.1)
   	script.Parent.Color = Color3.fromRGB(105,255,105)
   end
end

script.Parent.ClickDetector.MouseClick:connect(Click)

Hello, I think this should fix the issue of them not working simultaneously:

local Glass = workspace.SGGlassWindows:GetChildren()
local Turn = true
function Click()
	coroutine.wrap(function()
		for _,x in pairs(Glass) do
			if x.Name == "Glass" and x:IsA("Part") then
				if Turn == true then
					x.Transparency= 0.65 
					wait()               
					x.Transparency= 0.1
					wait()
					x.Transparency= 0
				else
					x.Transparency= 0 
					wait()               
					x.Transparency= 0.1
					wait()
					x.Transparency= 0.65
				end
			end
		end
	end)()

	if Turn == true then
		Turn = false
		script.Parent.Color = Color3.fromRGB(105,255,105)
		wait(0.1)               
		script.Parent.Color = Color3.fromRGB(132, 140, 55)
		wait(0.1)
		script.Parent.Color = Color3.fromRGB(140, 54, 54)
	else
		Turn = true
		script.Parent.Color = Color3.fromRGB(140, 54, 54)
		wait(0.1)               
		script.Parent.Color = Color3.fromRGB(132, 140, 55)
		wait(0.1)
		script.Parent.Color = Color3.fromRGB(105,255,105)
	end
end

script.Parent.ClickDetector.MouseClick:connect(Click)

I would also highly suggest using TweenService for transitioning between the transparency and color. It would give the transitions an better effect and help shorten your code.

Documentation: TweenService | Documentation - Roblox Creator Hub

2 Likes

thank you for the code, however, this causes them to switch between the two now.

I have now fixed my issues, It seems that adding in tweens has since fixed this issue and works on a variety of other windows.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.