Click detector script not working (2)

Anybody know why this script isn’t working? Only some parts go transparent… (yes I checked the model, all the parts are in it)

open1.MouseClick:Connect(function(player)
	if player.Team == game.Teams["A Team"] then
		for i,v in ipairs(script.Parent.Parent.GarageDoor1:GetChildren()) do 
			wait(.02)
			v.Transparency = 1
			v.CanCollide = false
			wait(4)
			v.Transparency = 0
			v.CanCollide = true
		end
	end	
end)
open2.MouseClick:Connect(function(player)
	if player.Team == game.Teams["A Team"] then
		for i,v in ipairs(script.Parent.Parent.GarageDoor2:GetChildren()) do 
			v.Transparency = 1
			v.CanCollide = false
			wait(4)
			v.Transparency = 0
			v.CanCollide = true
		end
	end
end)

Sorry about the first one, had to re post)

Instead of doing this, you should create a function

local function Open1(Player)
       -- code here
end

event would be

Name.MouseClick:Connect(Open1)

Maybe this

local function open1(player)
	if player.Team == game.Teams["A Team"] then
		for i,v in ipairs(script.Parent.Parent.GarageDoor1:GetChildren()) do 
			coroutine.wrap(function()
                wait(0.2)
			    v.Transparency = 1
			    v.CanCollide = false
			    wait(4)
			    v.Transparency = 0
			    v.CanCollide = true
           end)()
		end
	end	
end

open1.MouseClick:Connect(open1)

local function open2(player)
	if player.Team == game.Teams["A Team"] then
		for i,v in ipairs(script.Parent.Parent.GarageDoor2:GetChildren()) do 
            coroutine.wrap(function()
			    v.Transparency = 1
			    v.CanCollide = false
			    wait(4)
			    v.Transparency = 0
			    v.CanCollide = true
           end)()
		end
	end
end

open2.MouseClick:Connect(open2)

The script still doesn’t work sadly…

Could you show me your output errors.

there are no errors… that’s why it’s confusing

Did you try print checks? could you print every line for me with numbers see where it stops

none of them are printing… weird

Is this a GUI or clickdetector?

Is this a GUI or clickdetector?

I am using a click detector.

Try this?

local Teams = game:GetService("Teams")

open1.MouseClick:Connect(function(player)
	if player.Team == Teams["A Team"] then
        print("Found a player who has a valid team")

		for i,v in ipairs(script.Parent.Parent.GarageDoor1:GetChildren()) do 
            print("Iterating through the loop", v)
			wait(.02)
			v.Transparency = 1
			v.CanCollide = false
			wait(4)
			v.Transparency = 0
			v.CanCollide = true
		end
	end	
end)
open2.MouseClick:Connect(function(player)
	if player.Team == Teams["A Team"] then
        print("Found a player who has a valid team")

		for i,v in ipairs(script.Parent.Parent.GarageDoor2:GetChildren()) do 
            print("Iterating through the loop", v)
			v.Transparency = 1
			v.CanCollide = false
			wait(4)
			v.Transparency = 0
			v.CanCollide = true
		end
	end
end)

Also side note, but your wait(4) is slowly going through all parts inside the loop which could be the reason why “some parts” are going transparent, as it’s going extremely slow

Might wanna wrap it in a coroutine of some sort

You could try


local CD = clickdetector information here


local function Open1(Player)
       -- code here
end

CD.MouseClick:Connect(Open1)

open1 is the CS in your script… So I don’t think it’d make a difference changing it to CD

the script works but it doesn’t do it for all the parts. Ill try removing the wait()

Yep, it works… I removed the wait()… but now how do I create a wait method for the wall to go back to normal after 4 seconds

um alright, so quick question you said that some part only go transparent? my question is well do you want all parts in the model to be transparent at the same time? since the loop sets the transparency to 1 then waits 4 seconds for it go to back for every 1 irrigation on a part so the loop is gonna yield every 4 seconds and set it back. my solution for this is add another for i loop to set it back.

here what i mean by adding another for i loop

local function Clicked(Player)
    for _,v in pairs(Model:GetChildren()) do
           if v:IsA("BasePart") then --checks if its a part
                 v.Transparecy = 1
          end
    end
   wait(4)
   for _,v in pairs(Model:GetChildren()) do
           if v:IsA("BasePart") then --checks if its a part
                 v.Transparecy = 0
          end
    end
end

ClickDectector:Connect(Clicked)

(im sorry if you didn’t undstand meh im kinda new here.)

do in pairs instead of ipairs, i think that will work

Works perfectly fine for me. As shown here,

I normally wouldn’t do wait, but this is just for you to understand.

local Teams = game:GetService("Teams")
local CD = game.Workspace.Garage.ClickDetector
local Garage = CD.Parent

local function Open1(Player)
	if Player.Team == Teams.Red then
		for i,v in pairs(Garage:GetChildren()) do
			if v:IsA("Part") then
				v.Transparency = 1
				Garage.Transparency = 1
			end
		end
		wait(4)
    for i,v in pairs(Garage:GetChildren()) do
			if v:IsA("Part") then
				v.Transparency = 0
				Garage.Transparency = 0
			end
		end	
	end
end

CD.MouseClick:Connect(Open1)

Here is a gif https://gyazo.com/d53a6b1ceee82e647e9684925f9382db

As the above post mentioned You could probably just create 2 loops for each function when you click on the detectors:

local Teams = game:GetService("Teams")
local Model1 = script.Parent.Parent.GarageDoor1
local Model2 = script.Parent.Parent.GarageDoor2

open1.MouseClick:Connect(function(player)
	if player.Team == Teams["A Team"] then
        print("Found a player who has a valid team")

		for i,v in ipairs(Model1:GetChildren()) do 
			v.Transparency = 1
			v.CanCollide = false
		end
        wait(4)
		for i,v in ipairs(Model1:GetChildren()) do 
			v.Transparency = 0
			v.CanCollide = true
		end

	end	
end)
open2.MouseClick:Connect(function(player)
	if player.Team == Teams["A Team"] then
        print("Found a player who has a valid team")

		for i,v in ipairs(Model2:GetChildren()) do 
			v.Transparency = 1
			v.CanCollide = false
		end
        wait(4)
		for i,v in ipairs(Model2:GetChildren()) do 
			v.Transparency = 0
			v.CanCollide = true
		end

	end
end)

Also @TFlanigan pairs/ipairs wouldn’t make any difference I believe

  • ipairs returns back the table in the ordered fashion it was delivered in

  • pairs just returns back the table in a randomized way

ipairs stops when it encounters a nil and they both work in the same order


image

1 Like