Creating a Player-Specific Script

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:
image

Here is the workspace:
image

Thank you for any help & sorry if it is a simple mistake. :slightly_smiling_face:

4 Likes

LocalScripts don’t function under Workspace.
You need to have it under PlayerScripts or something client-side.

This would also mean reassigning some variables to make it work.

8 Likes

Would it work in a reg script?

1 Like

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.

Instance.MouseClick:Connect(function(Player)

end)
2 Likes

It can work with a regular script. Just follow what @desinied said.

1 Like

I think you have to put if player.UserId in the onClicked script

1 Like

You should localize all your functions and variables, I don’t understand why you localized Players but not door1 or door2

1 Like

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.

1 Like

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)
5 Likes

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.

Actually, I fixed it, nevermind. Thanks for everyone’s help.

1 Like