ClickDetector won't start the Tweening script

Greetings Developers.
I wanted to make a Slide Door using a ClickDetector and a tweening script with CFrame. Before i added the ClickDetector elements in the script, the tween worked without problem but now it doesnt tween when i click the detector. There is no error in the output while clicking.
Me, being a beginner scripter, can’t see whats wrong here…
Here is the script :

local TweenService = game:GetService("TweenService")

local Panel = workspace.SlidingDoor
local PanelRoot = Panel.PrimaryPart

local PanelSlideInfo = TweenInfo.new() -- Let's use all defaults here

local button = Panel.Part.ClickDetector

local open = false
button.MouseClick:Connect(function()
if open == false then
	local PanelSlideTween = TweenService:Create(PanelRoot, PanelSlideInfo, {
    	CFrame = PanelRoot.CFrame * CFrame.new(PanelRoot.Size.X + 0, 0, 5.3)
	})
	PanelSlideTween:Play()

	open = true

else
	local PanelSlideTween = TweenService:Create(PanelRoot, PanelSlideInfo, {
    	CFrame = PanelRoot.CFrame * CFrame.new(PanelRoot.Size.X + 0, 0, -5.3)
	})
	PanelSlideTween:Play()
	end
end)

Thank you everyone for helping :heart:

4 Likes

Have you tried printing out something when the ClickDetector gets clicked and open is false?

Yes i did and i got the same results. Both printing and open false or true

This is the script right now and its still not working

local TweenService = game:GetService("TweenService")
local clickdetector = script.Parent:WaitForChild("ClickDetector")
local button = script.Parent
local Panel = workspace.SlidingDoor
local PanelRoot = Panel.PrimaryPart

local PanelSlideInfo = TweenInfo.new() -- Let's use all defaults here

	local PanelSlideInTween = TweenService:Create(PanelRoot, PanelSlideInfo, {
    	CFrame = PanelRoot.CFrame * CFrame.new(PanelRoot.Size.X + 0, 0, 5.3)})

	local PanelSlideoutTween = TweenService:Create(PanelRoot, PanelSlideInfo, {
    	CFrame = PanelRoot.CFrame * CFrame.new(PanelRoot.Size.X + 0, 0, -5.3)})
local open = false
button.MouseClick:Connect(function()
if open == false then

	PanelSlideInTween:Play()
	print("Open door!")

	open = true

else

	PanelSlideoutTween:Play()
	print("Close door!")
	
	open = false
	end
end)

Maybe fire a remote event? Then later, use an OnClientEvent function

1 Like

I had the same issue with FilteringEnabled because I did not realise that it also affected ClickDetectors and SurfaceGuis. I did what Crrustyy described.

Something like this will work.
LocalScript in ReplicatedFirst:

local player= 				game.Players.LocalPlayer
local mouse=				player:GetMouse()
local UserInputService=		game:GetService("UserInputService")
local RemoteEvent=			game.ReplicatedStorage:WaitForChild("RemoteEvent")

function ClickDetectorSearch()
	print(mouse.Target)
	if mouse.Target then
		--Is nil when aiming at the skybox
		if mouse.Target:FindFirstChild("ClickDetector") then
			--Part has a ClickDetector
			if player.Character then
				if player.Character:FindFirstChild("HumanoidRootPart") then
					--Error checking to ensure the player is spawned with an existing HumanoidRootPart
					if (player.Character.HumanoidRootPart.Position - mouse.Target.Position).magnitude <= mouse.Target.ClickDetector.MaxActivationDistance then
						--Player is within max activation distance
						RemoteEvent:FireServer("ClickDetector Input", mouse.Target.ClickDetector)
					end
				end
			end
		end
	end
end

function InputBegan(input,gameProcessed)
	if gameProcessed then return end
	
	if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
		ClickDetectorSearch()
	end
end
	
UserInputService.InputBegan:connect(function(input,gameProcessed)
	InputBegan(input,gameProcessed)
end)

You’d then have a script handle the event. Also add a RemoteEvent.

local TweenService= 		game:GetService("TweenService")
local RemoteEvent=			game.ReplicatedStorage:WaitForChild("RemoteEvent")

RemoteEvent.OnServerEvent:connect(function(player, define, ...)
	if define == "ClickDetector Input" then
		local clickdetector = ...
		
		local button = script.Parent
		local Panel = workspace.SlidingDoor
		local PanelRoot = Panel.PrimaryPart
		
		local PanelSlideInfo = TweenInfo.new() -- Let's use all defaults here
		
			local PanelSlideInTween = TweenService:Create(PanelRoot, PanelSlideInfo, {
		    	CFrame = PanelRoot.CFrame * CFrame.new(PanelRoot.Size.X + 0, 0, 5.3)})
		
			local PanelSlideoutTween = TweenService:Create(PanelRoot, PanelSlideInfo, {
		    	CFrame = PanelRoot.CFrame * CFrame.new(PanelRoot.Size.X + 0, 0, -5.3)})
		local open = false
		button.MouseClick:Connect(function()
		if open == false then
		
			PanelSlideInTween:Play()
			print("Open door!")
		
			open = true
		
		else
		
			PanelSlideoutTween:Play()
			print("Close door!")
			
			open = false
			end
		end)
	end
end)

There are probably other ways to do it but that’s how I handle it.

Have you checked if your tween works without the ClickDetector?

The print mouse.Target works since i can see in the output everything i click but the door is still not tweening.
All i did was changing the Remote Event’s name and define it in the scripts and also tried moving the local clickdetector inside and outside the function and its still not working. I am truly sorry if i don’t understand something


I saw “Clickdetector Input” and i thought it should be “Door” , since when i click on the door with click detector in it it says Door in my output. However, i tried with and without the “Clickdetector Input” and got the same result

@ThePoisonFish Yes. I wanted to make sure the tweening was perfect before i add the Click Detector. Firstly i tried to tween it using a Vector3 but i saw that the handle was stil in place even tho it was anchored to the tweened door so i checked the hub. There is where i saw that i need CFrame in order to tween models.

My apologies, I did not look at your code. I just copy and pasted it into my own code assuming it’d work.
Try this, it should work.

local TweenService= 		game:GetService("TweenService")
local RemoteEvent=			game.ReplicatedStorage:WaitForChild("RemoteEvent")

local Panel = 				workspace.SlidingDoor
local PanelRoot = 			Panel.PrimaryPart
local DoorOpen = 			false
local DoorClosedCFrame = 	PanelRoot.CFrame
local DoorOpenCFrame = 		PanelRoot.CFrame * CFrame.new(PanelRoot.Size.X + 0, 0, 5.3)

RemoteEvent.OnServerEvent:connect(function(player, define, ...)
	if define == "ClickDetector Input" then
		local clickdetector = ...
		
		local button = script.Parent
		
		local PanelSlideInfo = TweenInfo.new() -- Let's use all defaults here
		

		local PanelSlideInTween = TweenService:Create(
			PanelRoot,
			TweenInfo.new(),
			{
				CFrame = DoorOpenCFrame
			}
		)
		local PanelSlideoutTween = TweenService:Create(
			PanelRoot,
			TweenInfo.new(),
			{
				CFrame = DoorClosedCFrame
			}
		)
		
		if DoorOpen then
			print("Closing door")
			PanelSlideoutTween:Play()
		else
			print("Open door")
			PanelSlideInTween:Play()
		end
		DoorOpen = not DoorOpen -- If true, make false. If false, make true.
	end
end)

You may have to mess around with the CFrame offset but that should work. The main problem in the code was that the script was attempting to run button.MouseClick() which I should have removed. The RemoteEvent function ‘ClickDetector Input’ does this instead now.

Note: I assumed that the open/closed CFrames of the door are constant as I’d wager the door is anchored and stationary. So, I saved those 2 CFrames at the start of the script as ‘DoorClosedCFrame’ and ‘DoorOpenCFrame’ instead of calling them every time the function is run. This isn’t necessary I just did it anyway.

1 Like

It’s okay, we all make mistakes.
But honestly, i am purely confused. I tried to see if the code works exactly as you sent it.
Then i put the CFrames i had in the last code for the position of the door when closing and opening./
Then i defined the local clickdetector = . . . as workspace.SlidingDoor.Door.ClickDetector (the actual position of it).
I tried so many ways to do it and im amazed how much script it needs to tween a CFrame model when clicked on. As i said, i’m a beginner scripter but i understand most of the code you gave me. I still dont know why the door isn’t moving when clicked on.
Here is the script version i gave up at and came to ask for help. My bad for making u write entire scripts for a beginner like me.

local TweenService= 		game:GetService("TweenService")
local RemoteEvent=			game.ReplicatedStorage:WaitForChild("RemoteEvent")

local Panel = 				workspace.SlidingDoor
local PanelRoot = 			Panel.PrimaryPart
local DoorOpen = 			false
local PanelSlideInfo = TweenInfo.new()
local PanelSlideInTween = TweenService:Create(PanelRoot, PanelSlideInfo, {
		    	CFrame = PanelRoot.CFrame * CFrame.new(PanelRoot.Size.X + 0, 0, 5.3)})
		
local PanelSlideoutTween = TweenService:Create(PanelRoot, PanelSlideInfo, {
		    	CFrame = PanelRoot.CFrame * CFrame.new(PanelRoot.Size.X + 0, 0, -5.3)})
		
local clickdetector = workspace.SlidingDoor.Door.ClickDetector

RemoteEvent.OnServerEvent:connect(function(player, define, clickdetector)
	if define == "Door" then
		
		local button = script.Parent
		
		local PanelSlideInfo = TweenInfo.new() 
		

		local PanelSlideInTween = TweenService:Create(
			PanelRoot,
			TweenInfo.new(),
			{
				CFrame = PanelRoot.CFrame * CFrame.new(PanelRoot.Size.X + 0, 0, 5.3)})
			}
		)
		local PanelSlideoutTween = TweenService:Create(
			PanelRoot,
			TweenInfo.new(),
			{
				CFrame = PanelRoot.CFrame * CFrame.new(PanelRoot.Size.X + 0, 0, -5.3)})
			}
		)
		
		if DoorOpen then
			print("Closing door")
			PanelSlideoutTween:Play()
		else
			print("Open door")
			PanelSlideInTween:Play()
		end
		DoorOpen = not DoorOpen
	end
end)

Yes , i tried with the local clickdetector inside and outside the OnServerEvent function.
Same with "ClickDetector Input" , thinking that it should be what i get in the output which is “Door”. However i tried using it’s original form and the “Door” form and got the same negative results.
Thank you so so much for helping me tho :heart:

No problem and don’t be afraid to ask questions on the devforum. There are plenty of people willing to help. Like with any language, it’s necessary to ask questions to learn Lua.

The ‘ClickDetector Input’ versus ‘Door’ name is just semantics. Both will do the exact same thing as long as the code inside the LocalScript also uses ‘Door’ instead of ‘ClickDetector Input.’ It’s just a name to give to the FireServer() message sent to the server.

I also realised something embarassing. Apparently, there is now support for ClickDetector.MouseClick() even when FilteringEnabled is enabled. So, there is no need to use the RemoteEvent function like I suggested. Your original code was actually fine. The only reason it did not open/close the door and only closed the door was that you forgot to update the ‘open’ variable after the door was closed after the second ‘PanelSlideTween’ was played.

I’m new to answering these scripting support questions so it’s my bad for making you do circles instead of fixing something so trivial. In my defense though, the last time I used a ClickDetector was many years ago and back then it didn’t have FilteringEnabled support so I had to use a RemoteEvent. At least now you know how to use RemoteEvent and the very-similar RemoteFunction which are very important in the current age of ROBLOX where FilteringEnabled is the default.

This code, which is almost identical to what you originally posted, should work on its own. Put it inside a Script, preferably in ServerScriptService so only the server can view it and players can’t see or steal it.

local TweenService = game:GetService("TweenService")

local Panel = workspace.SlidingDoor
local PanelRoot = Panel.PrimaryPart

local PanelSlideInfo = TweenInfo.new() -- Let's use all defaults here

local button = Panel.Part.ClickDetector

local DoorOpen = 			false
local DoorClosedCFrame = 	PanelRoot.CFrame
local DoorOpenCFrame = 		PanelRoot.CFrame * CFrame.new(PanelRoot.Size.X + 0, 0, 5.3)


button.MouseClick:Connect(function()
	if DoorOpen == false then
		local PanelSlideTween = TweenService:Create(
			PanelRoot, 
			PanelSlideInfo, 
			{
	    	CFrame = DoorClosedCFrame
		})
		PanelSlideTween:Play()
	else
		local PanelSlideTween = TweenService:Create(
			PanelRoot, 
			PanelSlideInfo, 
			{
	    	CFrame = DoorOpenCFrame
		})
		PanelSlideTween:Play()
	end
	
	DoorOpen = not DoorOpen -- If true, make false. If false, make true.
end)
1 Like

It’s still not working. Also yes i just realised that in the original script i posted there was a variable for open to go from false to true but not from true to false. However, even with that mistake the door should open but not close. Instead it’s completly stuck. As i said, before i inserted the script and make sure the tween placement is perfect, the door was working.
So far i tried :
The exact script you showed me.
I replaced local DoorClosedCFrame and local DoorOpenCFrame with
local PanelSlideOutTween and local PanelSlideInTween and defined the Play() correctly and its still not working.

Right now i will try to remove everything from the actual panel, leaving it only a glass panel and i will see if i can move it using Vector3 tweening and click detector. If not i am afraid i will leave the doors for now.
Since you are helping me so much, this is basically what i want to do


Slide Doors. Each one should be able to slide behind or infront of the other one when clicked on. As you can see upon close look each panel has a handle and, if i use Vector3 it would move only the panel, while the handle stays frozen in the air even when not anchored and only welded to the main glass panel. Now as i said i will keep trying diferent ways of the script to make it work.

Unsure what the issue is but if you open the Output menu (View tab at top of screen – > Output) it should tell you your error. I just made this simple window and set the PrimaryPart of the model to be the glass so your ‘PanelRoot’ variable is the glass. The code is essentially the same as what I posted above and worked for me.


1 Like

I did the whole script again and got the same results.
I honestly see nothing wrong here and i get no error in the Output. The print doesn’t work too.

--Setting up the Tween
local TweenService = game:GetService("TweenService")
local Tweeninfo = TweenInfo.new() --Default
--Setting up the variables 
local SlidingDoor = game.Workspace.SlidingDoor
local SlidingPanel = SlidingDoor.PrimaryPart
local ClickDetector = SlidingDoor.Door.ClickDetector

--The ending points of tweening
local SlideInTween = TweenService:Create(SlidingDoor,Tweeninfo,SlidingPanel.Cframe * CFrame.new(SlidingPanel.Size.X + 0,0,5.3))
local SlideOutTween = TweenService:Create(SlideInTween,Tweeninfo,SlidingPanel.Cframe * CFrame.new(SlidingPanel.Size.X + 0,0,-5.3))

local doorOpen = false

--Creating the function activated upon click
ClickDetector.MouseClick:Connect(function()
	if doorOpen == false then
		SlideInTween:Play()
		doorOpen = true 
		print("Opening Door")
		
	else
		SlideOutTween:Play()
		doorOpen = false
		print("Closing Door")
	end
end)

I will try on a smaller baseplate too with the same model right now

Edit : I tried my code in an empty baseplate and it didn’t worked but yours did. Went back to the original game and tried your code once again in the sliding door and of course it did not worked. I even removed the Handle part who was connected to the sliding door through a Weld Constraint and got the same negative results. I am wondering what is the problem

Edit #2 : I made a quick “door” thing and inserted your script, changing the variables into the correct ones and this time is working.
After this i decied to put your script again into the actual door and now it works. Not as straight as intended but im glad that it works. By straight i mean this :
https://gyazo.com/004e75be650f9bc56955750f9648a3e2

But as i remember colbert2688’s tutorial shows something similar to what i have to do. Setting the Axis correctly. Hopefully i will be able to do that.

You have a few flaws in your last code posted, at least in these two lines, that I can spot:

local SlideInTween  = TweenService:Create(SlidingDoor, Tweeninfo,SlidingPanel.Cframe * CFrame.new(SlidingPanel.Size.X + 0,0, 5.3))
local SlideOutTween = TweenService:Create(SlideInTween,Tweeninfo,SlidingPanel.Cframe * CFrame.new(SlidingPanel.Size.X + 0,0,-5.3))

The flaws are in the details. And these are very specific details, that you should learn about.

Do please understand, that the TweenService:Create() method needs three arguments of the following types:

  • An instance that will be affected (usually not a Model, but a BasePart / Part)
  • A TweenInfo object
  • A “dictionary” (table) containing the property-names to affect, and what value to reach (goal).

1.
So your statement here is wrong, as the third argument is clearly not a “dictionary”, but only a CFrame:

TweenService:Create(SlidingDoor, Tweeninfo, SlidingPanel.Cframe * CFrame.new(SlidingPanel.Size.X + 0,0,5.3))

To “fix it”, you need to write the code as such (oh, I also changed the first argument):

TweenService:Create( SlidingPanel, Tweeninfo, { CFrame = SlidingPanel.Cframe * CFrame.new(SlidingPanel.Size.X + 0,0,5.3) } )

2.
Your second statement creating a tween, has even one more problem, this time with its first argument. Why do you suddenly want to tween the SlideInTween-object?

local SlideOutTween = TweenService:Create(SlideInTween, Tweeninfo, SlidingPanel.Cframe * CFrame.new(SlidingPanel.Size.X + 0,0,-5.3))

I guess it could have been a typing mistake, but the computer does not know that. So the statement should look like this:

local SlideOutTween = TweenService:Create( SlidingPanel, Tweeninfo, { CFrame = SlidingPanel.Cframe * CFrame.new(SlidingPanel.Size.X + 0,0,-5.3) } )

3.
These details were also there in colbert2677’s tutorial if you look very closely at his code samples.

From colbert2677’s tutorial i started the tween. Everything went well until i had to put the ClickDetector.
Now, after realizing my mistakes i corrected them and the door is still not sliding. Somehow, using @BRIANO10’s script in an empty baseplate, everything worked well. I checked if anything is doing that mistake. The SlidingDoor model is made out of the following components : image
The primary part is the door and the handle is not anchored and welded to the door.

1 Like

From that screenshot you have posted, to me it looks like that Script’s icon is more gray than blue.

So I wonder if you have set its Disabled property to true? - Thereby causing the script to never be run, and therefore never to set up the event-handler of the ClickDetector?

In cases like these, where “nothing works”, it becomes necessary to add more print statements, at several points in the code, for trying to get a better understand of, “where” the executed code at least has reached. - And if (some of) these prints never appear in the Output-panel, then you have gathered clues about where code statements was ‘not executed’, which in turn can help you with some deductive reasoning/thinking, that a (or the) problem must exist somewhere earlier.

Some simple print debugging statements, could be:

-- Just a simple debug print, expecting this to be printed to the Output-panel,
-- when starting game, and the script is being compiled/run for the first time.
print("[TRACE] Connecting `ClickDetector.MouseClick` to function") 

ClickDetector.MouseClick:Connect(function()
    -- A simple debug print, as _the_very_first_statement_
    -- within this anonymous function.
    print("[TRACE] Entered ClickDetector.MouseClick anonymous function") 

    if doorOpen == false then
      -- etc.
2 Likes