Help with making certain person only ClickDetector

Hello,

Currently working on a classic game, right now I’m making a garage door. I want the house to be rented out to people, so once I rent the house to them, only they can open the door.

(The garage door is a model from a friend)
Here’s the code:

if player.UserID = 994454489 then

local Handle = script.Parent.Handle
local open = false
local active = false

function click()
	if open == false and active == false then
		active = true
		for i = 1,60 do
			script.Parent:SetPrimaryPartCFrame(script.Parent.PrimaryPart.CFrame * CFrame.Angles(0, 0, math.rad(1.5)))
			wait()
			script.Parent:SetPrimaryPartCFrame(script.Parent.PrimaryPart.CFrame * CFrame.new(0.05,0,0))			
			wait()
		end
		active = false
		open = true
	elseif open == true and active == false then
		active = true
		for i = 1,60 do
			script.Parent:SetPrimaryPartCFrame(script.Parent.PrimaryPart.CFrame * CFrame.Angles(0, 0, math.rad(-1.5)))
			wait()
			script.Parent:SetPrimaryPartCFrame(script.Parent.PrimaryPart.CFrame * CFrame.new(-0.05,0,0))			
			wait()
		end
		active = false
		open = false
	end
end

Handle.ClickDetector.MouseClick:connect(click)

I’m a total doofus at scripting, so I just tried to make that work, but of course it doesn’t!

It’s most likely a beginners mistake, but I will still be thankful for any tips.

Thanks.

So my way of doing it (if it was correct) wouldn’t work?

if player.UserID = 994454489 then

local Handle = script.Parent.Handle
local open = false
local active = false

function click()
	if open == false and active == false then
		active = true
		for i = 1,60 do
			script.Parent:SetPrimaryPartCFrame(script.Parent.PrimaryPart.CFrame * CFrame.Angles(0, 0, math.rad(1.5)))
			wait()
			script.Parent:SetPrimaryPartCFrame(script.Parent.PrimaryPart.CFrame * CFrame.new(0.05,0,0))			
			wait()
		end
		active = false
		open = true
	elseif open == true and active == false then
		active = true
		for i = 1,60 do
			script.Parent:SetPrimaryPartCFrame(script.Parent.PrimaryPart.CFrame * CFrame.Angles(0, 0, math.rad(-1.5)))
			wait()
			script.Parent:SetPrimaryPartCFrame(script.Parent.PrimaryPart.CFrame * CFrame.new(-0.05,0,0))			
			wait()
		end
		active = false
		open = false
	end
end

This is highly inefficient and should not be done like this.

Also the c in Connect is not capitalized

1 Like

Okay, but this isn’t the most constructive comment. Any tips on how to make it more efficient?

(Thanks for showing me the uncapitalized C by the way)

You can try something like this:

local enabledDoor = false

local function onClick()
	if (enabledDoor) then
		enabledDoor = false
		-- code to disable the door state
	else
		enabledDoor = true
		-- code the enable the door state
	end
end

You create a function to detect when the ‘button’ is clicked, if the door is enabled then disable it, and vias versa.

Edit: Also I noticed that you’re using :SetPrimaryPartCFrame() but you should use :PivotTo(). :SetPrimaryPartCFrame() has float point errors and will slowly make the parts parented to the model move away by a very small amount, while :PivotTo() doesn’t do that and works long term perfectly (if the documentation stated it correctly).

1 Like

The example by @0stonze was perfect for this:

local clickDetector = -- Path to you ClickDetector
local playerCanClick = -- Path to the player who can click the ClickDetector

clickDetector.MouseClick:Connect(function(playerWhoClicked)
      if playerCanClick == playerWhoClicked then
        -- Insert the code you want the script to execute here
      end
end)

However you should add a connection for efficiency so something like this:

local clickDetector = -- Path to you ClickDetector
local playerCanClick = -- Path to the player who can click the ClickDetector

local connection

local function click(playerWhoClicked)
  if playerCanClick == playerWhoClicked then
      -- Insert the code you want the script to execute here
      -- In the end of the code you put here, do this:
      connection:Disconnect()
  end
end)

connection = clickDetector.MouseClick:Connect(click)

This will make it a more efficient

1 Like

Okay, but where in this script does it recognize which player should be able to open the door, and which doesn’t?

As I explained in my post, I basically want only ONE person to be able to open the door

Oh, so “playerWhoClicked” should be the username of the user I want to be able to open the door? Sorry for the tons of questions, I really am a doofus at scripting.

Right, but where do I define what player should be able to open the door?

GOD DAMN, Got it! Okay, I’ll see if I can get this to work

So at “playerCanClick”, should I put the username there? Nevermind, I think it’s after the =

god damn i’m stupid

1 Like

No, but if you want to put the player user id which will be the most accurate do this:

local clickDetector = -- Path to your ClickDetector

local playerCanClick = -- Path to the player who can click

clickDetector.MouseClick:Connect(function(playerWhoClicked)
    if playerCanClick.UserId == playerWhoClicked then
        print("Success!")
    end
end)

If you put the playerName in playerWhoClicked if 2 players have the same name it will allow both players to click the part.

If you still don’t want to do the code I put above do this:

local clickDetector = -- Path to you ClickDetector
local playerCanClick = -- Path to the player who can click the ClickDetector

local connection

local function click(playerWhoClicked)
  if playerCanClick == playerWhoClicked then
      -- Insert the code you want the script to execute here
      -- In the end of the code you put here, do this:
      connection:Disconnect()
  end
end)

connection = clickDetector.MouseClick:Connect(click)

I recommend you use the 2nd code block I gave since it also removes the function from memory when it is not needed!

1 Like

You did not put anything for the clickdetector variable. And you have an indentation mistake as well

Even if they can’t be the same they should not be used. UserId is meant and safer for this, just in case Roblox decides to change .Name and multiple users can have the same name the code will glitch

1 Like

Like this?
local ClickDetector = workspace.Garage.Handle

Yes the path should be a click detector.

Something like:

local clickDetector = workspace.Garage.Handle

Be sure Handle is a clickdetector!

1 Like

Oh, well then it’s this:
local ClickDetector = workspace.Garage.Handle.ClickDetector

Because the ClickDetector is inside the part called “handle”

1 Like

Yes, it should be like that

char limit

1 Like