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.
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.
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.
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
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)
–
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
All the best!
Bit of explanation if you want more info: Documentation - Roblox Creator Hub