Problem:
I am currently working on creating a CFrame sliding door (I know I should use tweening), but I want the button to be accessible by a person with a certain userID/username. I have created the script, which I thought would work, but for some reason it doesn’t. The script below is located in a local script.
Here is the script:
Here is the workspace:
Thank you for any help & sorry if it is a simple mistake.
You are not able to use a click detector with a LocalScript from the workspace, the MouseClick function of a ClickDetector should be handled in this case on the server, which is done by a regular script.
You also would instead of having the if statement outside of the function, add it inside. You can’t call game.Players.LocalPlayer from a server script, so you will have to use the Player argument in the MouseClick function.
What do you mean?
They’re in the same script, do you mean putting a UserId check in the function?
Also, LocalScripts can’t function and won’t under Workspace, as ShadokuSan said.
So the first thing that’s already been said, a LocalScript will not function under the Workspace. In order to connect to ClickDetectors from the client, you must place it in a client-based container. The containers where LocalScripts run are found on its documentation page. For now, change this to a regular Script.
Once that’s done, you need to switch where you place the if statement. Instead of wrapping MouseClick with the UserId check, put it inside your click function. If you check ClickDetector.MouseClick’s page, it fires with the player who activated the clicker. Therefore, we check the UserId against this player.
Here’s a code template which incorporates the above feedback:
local door = script.Parent.Parent.Door
local door2 = script.Parent.Parent.Door2
local function onClicked(player)
if player.UserId == 325345634 then
-- Can use. Put the rest of the door movement code here.
end
end
script.Parent.ClickDetector.MouseClick:Connect(onClicked)
Thank you, this works, but now the door “teleports” open instead of sliding. I tried changing the z value to solve this, which just makes the door teleport further, still not sliding.