Gate script not working

Try this one:

function LeftClick()

--your stuff here

end

script.Parent.MouseButton1Click:Connect(LeftClick)

its useless now there is a solution

Hey, so I posted above - But I don’t think you saw it!

Here’s what I said.

Anyway, now looking at the model - I can easily help you out here.

  • First of all, I want you to put the parts inside of a model called “GateParts”
  • After this, I want you to define the GateParts within a new script which is just in the model, none of the “script.parent.parent.parent” nonsense, along with the button.
local sModel = script.Parent
local GateParts = sModel .Gateparts -- This variable get's all of the parts within that model
local Button = sModel.Button

Your code should look like that so far

Now, for the other steps!

  • Now we want to detect when a player clicks the part, first off all we want you get the button variable we defined above (Button) - And detect when the ClickDector inside has been clicked.

  • And then, we want to create another function called “OpenGate”

Use the links I’ve linked below to help!

local function ControlGate()
     -- We have our function created, let's go!
end

Button.ClickDetector.MouseClick:Connect(ControlGate)

So now your code so far should look like:

local sModel = script.Parent
local GateParts = sModel .Gateparts
local Button = sModel.Button

local function ControlGate()
     -- We have our function created, let's go!
end

Button.ClickDetector.MouseClick:Connect(ControlGate)

We’re getting there!

Now, I want you to read up on loops.

  • Once you have read the link about loops above, try doing a loop that will make all of your parts within the gate model turn transparent with “CanCollide” false!

Now your code should look like this.

local sModel = script.Parent
local GateParts = sModel .Gateparts
local Button = sModel.Button

local function ControlGate()
    for _,v in pairs(GateParts:GetChildren()) do
         v.Transparency = 1
         v.CanCollide = false
    end
end

Button.ClickDetector.MouseClick:Connect(ControlGate)

Great, now we have (or should’ve) a working gate!

Although they are still some issues we could encounter:

  • What about closing the gate?
  • What about if you put an object in there which isn’t a part?

Let’s fix this.

  • Firstly, read up on what a “ClassName” is:
  • After you have read this, try and make it so only the ClassNames “Part” & “MeshPart” is shown!

It should look like this:

local function ControlGate()
    for _,v in pairs(GateParts:GetChildren()) do
         if v.ClassName == "Part" or v.ClassName == "MeshPart" then
            v.Transparency = 1
            v.CanCollide = false
        end
    end
end

But wait, this doesn’t fix the issue on if the door is already open or not

This will be our next step

  • Now we need another variable, called “GateState” - Or whatever you want to name it, feel free to be creative. Oh, and we need a variable called “GateCoolDown” - This is how long you want to wait before the door can be opened again. - Also be sure to add a “CanBeChanged” variable so we can see if we can change the state of the door or not at that time!

  • With this, we’ll be doing a simple denounce effect - I personally use os.Time() rather than a variable denounce, but it isn’t needed for this - And we can talk about that another time. (Unless you want to look it up, feel free - Linked below!)

This is what your final code should look like - I hope it helped!

local sModel = script.Parent
local GateParts = sModel .Gateparts
local Button = sModel.Button
local GateState = false -- false as in it's closed!
-- Debounce variables
local CanBeChanged = true
local GateCoolDown = 1 -- This is in seconds!

local function ControlGate()
	if CanBeChanged then
	CanBeChanged = false
		if not GateState then
			GateState = not GateState
			for _,v in pairs(GateParts:GetChildren()) do
			v.Transparency = 1
			v.CanCollide = false
			end
		else
			GateState = not GateState
			for _,v in pairs(GateParts:GetChildren()) do
			v.Transparency = 0
			v.CanCollide = true
			end
		end
	end
	wait(GateCoolDown)
	CanBeChanged = true
end

Button.ClickDetector.MouseClick:Connect(ControlGate)

Although this work’s perfectly fine for what you are doing, I personally would put all of the gates in a folder called “Gates” with each gate having its own model called “Gates” and have a script which loops through each time a click detector is clicked, (Which is located in ServerScriptStorage)

But what about detecting which gates are open, and their cooldown?

We would put the gates which are open into a table, and detect if they’re open along with a time until they can next be opened using os.time() and remove them from the table after if the current time is X amount of seconds ahead since the time the door was opened - But, you don’t have to worry about that - Unless you want to :eyes: if so, I’ve placed some links down below to help you!

Read up on os here

1 Like