How do I make a door open only for one person?[Solved]

Screenshot_49

How would I make this door only open for one person?
To Clarify the door works perfectly fine, however I only want it to be open on one persons screen, the person who opened the door.

I tried replacing the script with a localscript with the same block of code inside but it didn’t seem to work.

Here is the script for the door.

local db = true

function onClicked()

if db == true then

db = false

script.DoorSound:Play()

for i = 1,75 do

script.Parent.CFrame = script.Parent.CFrame * CFrame.new(0, 0.1, 0) 
wait(0.01) 

end

wait(2)

script.DoorSound:Play()

for i = 1,75 do

script.Parent.CFrame = script.Parent.CFrame * CFrame.new(0,-0.1, 0)
wait(0.01) 

end

	db = true
end

end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

4 Likes

Did you put the LocalScript inside of StarterPlayer.StarterPlayerScripts? LocalScripts in workspace won’t run.

1 Like

How would I make the door open If I put the localscript inside of Starter Player Scripts ? I’m relatively new so I’m confused on how you would do that.

1 Like

Paste all of your code into a LocalScript located inside of StarterPlayerScripts with few changes. Some of them include:

  • Replacing script.Parent with workspace.Lobby.OuterHallways.HallwayDoorway1.Doorway.Door
  • Placing DoorSound into the door and replacing the code for it
  • You should also replace :connect with :Connect because the lowercase one is deprecated.

Also you can put triple backtics (` is a backtick) around your code to make it easier to read.

2 Likes

Thanks I’ll try this, I’ll have to rename my doors probably since I have multiple

1 Like

I tried what you said but it didn’t work, maybe I’m doing something wrong ? here’s the changes I made

Screenshot_50

Screenshot_51

Script

local db = true
local Door1Open = workspace.Lobby.OuterHallways.HallwayDoorway1.Doorway.Door1

function onClicked()

if db == true then

db = false

Door1Open.DoorSound:Play()

for i = 1,75 do

Door1Open.CFrame = Door1Open.CFrame * CFrame.new(0, 0.1, 0) 
wait(0.01) 

end

wait(2)

Door1Open.DoorSound:Play()

for i = 1,75 do

Door1Open.CFrame = Door1Open.CFrame * CFrame.new(0,-0.1, 0)
wait(0.01) 

end

	db = true
end

end

Door1Open.MouseClick:Connect(onClicked)

1 Like

Don’t make it a local script.
Click detectors provide the player who clicked.
Just check to see if that person is the one allowed before running your code.

1 Like

Wait so if I added something in my code that detected the first child who clicked the door wouldn’t it still open for everyone if it’s not a local script ? I’m confused.

The LocalScript must indeed be a child of StarterPlayerScripts.
It is best to use RunService.RenderStepped:Wait() and not wait(0.01) so it cleanly moves the door every time a frame is rendered. (Will move 60 times in one second)

Here, I have attempted to clean up your code and get it working.

local RunService = game:GetService("RunService")

local Door1Open = workspace:WaitForChild("Lobby"):WaitForChild("OuterHallways"):WaitForChild("HallwayDoorway1"):WaitForChild("Doorway"):WaitForChild("Door1")
local db = true


local function toggleDoor(tog)
    Door1Open.DoorSound:Play()
    for i = 1,75 do
        Door1Open.CFrame = Door1Open.CFrame * CFrame.new(0, tog, 0) 
        RunService.RenderStepped:Wait()
    end
end
    

local function onClicked()
    if db == true then
        db = false
        toggleDoor(0.1)
        wait(2)
        toggleDoor(-0.1)
    end
    db = true
end

Door1Open.MouseClick:Connect(onClicked)

so you want the door to only show as open for one person, the person who clicked it?
but anyone can click it and it open?

Yes I want the person who opened the door to have it appear open but to everybody else it should appear closed until they click it

My first thought is that there should be a different door for each client with local scripts to make that work. I would need to test it though.

There is no door → client adds door with function.

Here is an example using a block. Put it in a Local script in StarterPlayerScripts

Simple Repro using a block and colors
 part = Instance.new("Part")
 part.Size = Vector3.new(1,1,1)
 part.Position = Vector3.new(2, 0.5, 2)
 clickDetector = Instance.new("ClickDetector")
 clickDetector.Parent = part



 function onClicked(player)
 	part.Color = Color3.new(0,0,0)
 end

 part.ClickDetector.MouseClick:Connect(onClicked)

 part.Parent = workspace

The physics is simulated since the client has ownership of the players character.
And the click effect only shows in that specific client since the object and therefore function are not shared with anyone else.

Sorry for the lacking example. I have got to go right now.

local thatPerson = {"Dev_0V", "maybe_someone_elseToo"}
--Use a datastore if you don't want to risk exploiters messing around with the above list

function onClicked(hit)
   local Array = {Value,OtherValue}
   for i = 1,#thatPerson do
     local plr= game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
     if plr.Name == thatPerson[I] then
           -- open door
           script.DoorSound:Play()
           for i = 1,75 do
             script.Parent.CFrame = script.Parent.CFrame * CFrame.new(0, 0.1, 0) 
             wait(0.01) 
           end
         wait(2)
         script.DoorSound:Play()
         for i = 1,75 do
           script.Parent.CFrame = script.Parent.CFrame * CFrame.new(0,-0.1, 0)
           wait(0.01) 
         end
           end
       end
   end
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)
1 Like

You might wanna use a tween service
to open the door…

Hey uh… I think this can help you.

Try this:


permission = { “Ron_GM” } --change this with your name

function checkOkToLetIn(name) 
	for i = 1,#permission do 
		if (string.upper(name) == string.upper(permission[i])) then return true end 
	end 
	return false 
end 

local Door = script.Parent

function onTouched(hit) 
	print("Door Hit") 
	local human = hit.Parent:findFirstChild("Humanoid") 
	if (human ~= nil ) then 
		print("Human touched door") 
		if (checkOkToLetIn(human.Parent.Name)) then 
			print("Human passed test") 
			Door.Transparency = 0.8 
			Door.CanCollide = false 
			wait(1.5)
			Door.CanCollide = true 
			Door.Transparency = 0 
		else human.Health= 0 -- delete this line if you want a non-killing VIP door 
		end 
	end 
end 

script.Parent.Touched:connect(onTouched)

Code executed on LocalScripts will only execute on the client (the player that is connected).

Since you want a door to open for only one player, consider a LocalScript listening for the MouseClick event and open/close the door locally instead of on the server. This will require that your localscript is a descendant of a service owned by the client or that the client can see (StarterPlayerScripts, for instance. Workspace will not work, as explained before.)

Your code would look something like this

-- LocalScript inside StarterPlayerScripts
local door = workspace.Path.To.Your.Door
local clickDetector = door.ClickDetector
-- more variables to handle logic

-- The code in these functions (Tweening a part, for example) will only run
-- on the local client. The changes made to the door in this code will not be replicated
-- to the server, and thus other players (read: clients) will not see them.

local function openDoor()
    -- Code to open door here
end

local function closeDoor()
    -- Code to close door here
end

clickDetector.MouseClick:Connect(function()
    -- Logic goes here
    if doorOpen then
        closeDoor()
    else
        openDoor()
    end
end)

I’ve created a small demonstration showing how this might work based on what you’re asking.

Apologies if the gif is low quality/framerate, hopefully it’s visible.
You said the door opens fine, so I’ll leave that to you. Assuming you’re talking about smoothly opening/closing it or whatever. I can help explain that if you want as well :slight_smile:
All the best!
Bit of explanation if you want more info: Documentation - Roblox Creator Hub

2 Likes