Assigning two functions to a ClickDetector

Hello People!
Kuro here, aka Immortxl from my past account.

I am currently learning how to script and was wondering if there are any scripters out there that could clear my confusions and answer my questions. I will be editing the questions as I further progress and will also leave my disxord here,
βΈΈ 𝕴𝖒𝖒𝖔𝖗𝖙𝖝𝖑#0666

Current Question :

How do i assign 2 functions to a click detector, like, click once to open a door, and click again to close that door. Something like that.

Thank you!

3 Likes

Category changed to #help-and-feedback:scripting-support.

To set two functions into one single click detector, it’s easier than it looks:

ClickDetector.MouseClick:Connect(function()
    firstFunction()
    secondFunction()
end)
4 Likes

OOOoooOo im gonna try that! I will let you know if it works, hold on :slight_smile:

1 Like

it wont work for me, maybe i did something wrong? have a look at the script here

helo = script.Parent
work = game.Workspace
erm = game.SoundService
local debounce = false

helo.ClickDetector.MouseClick:Connect(function()

firstFunction(function()
if not debounce then
debounce = true

  work.Level.Lever.Position = Vector3.new(618.4, 8.675, 35.525)
  work.Level.Lever.Orientation = Vector3.new(0, 0, -30)
  work.REDTEAM.Transparency = 1
    work.REDTEAM.CanCollide = false
  erm.Flick:Play()
  
  wait(10)
  
  debounce = false
  
  end

end)

  secondFunction(function()
  	if not debounce then
  		debounce = true

  		work.Level.Lever.Position = Vector3.new(615.85, 8.675, 35.525)
  		work.Level.Lever.Orientation = Vector3.new(0, 0, 45)
  		work.REDTEAM.Transparency = 0
  		work.REDTEAM.CanCollide = true
  		erm.Flick:Play()

  		wait(10)

  		debounce = false

  	end
  end)

end)

1 Like

For this purpose you could just store a bool value on the door and pass its value into a function to open or close the door:

function ToggleDoor(value)
    if value then
        --CloseDoor
    else
        --OpenDoor
    end
end

You defined the event above the functions place this part at the end of your script

helo.ClickDetector.MouseClick:Connect(function()
1 Like

oooo that could workk, tho im making something harder…

You’re supposed to substitute the function with other functions, besides the syntax is incorrectly written. Some of the code could be improved as this object is statically stuck in one place and cannot be changed without breaking its level away from its socket.

local object = script.Parent
local erm = game.SoundService
local debounce, isOpen

object.ClickDetector.MouseClick:Connect(function()
	if debounce then
		return
	end
	debounce = true

	if not isOpen then
		workspace.Level.Lever.Position = Vector3.new(618.4, 8.675, 35.525)
		workspace.Level.Lever.Orientation = Vector3.new(0, 0, -30)
		workspace.REDTEAM.Transparency = 1
		workspace.REDTEAM.CanCollide = false
		
		isOpen = true
	else
		workspace.Level.Lever.Position = Vector3.new(615.85, 8.675, 35.525)
		workspace.Level.Lever.Orientation = Vector3.new(0, 0, 45)
		workspace.REDTEAM.Transparency = 0
		workspace.REDTEAM.CanCollide = true
		
		isOpen = false
	end
	
	erm.Flick:Play()
	wait(10)
	debounce = false
end)
  • workspace is actually a global.
  • Statements are like A or B switches, use them wisely
  • Controls of the statements requires a boolean which reflects A or B gate
  • Positions are fixated and needs to be relative positions to X rather than constants
1 Like