LocalScript Way
You can declare a boolean variable called isAuthorized
and change it in one of your custom function, and then use a conditional statement that checks if the player is authorized to use the lightswitch, something like this:
01 | local player = game.Players.LocalPlayer |
02 | local isAuthorized = false |
03 | function togglePlayerAuth() |
04 | if not isAuthorized then |
11 | game.Workspace.YourLightswitch.MouseClick:connect( function (p) |
12 | if not isAuthorized then |
Script Way
You can declare a BoolValue called "isAuthorized" this too, same way of the LocalScript way.
01 | function onPlayerLogin(p) |
02 | local tempdata = p:FindFirstChild( "TempData" ) |
04 | tempdata = Instance.new( "Model" , p) |
05 | tempdata.Name = "TempData" |
07 | local isAuthorized = tempdata:FindFirstChild( "IsAuthorized" ) |
08 | if not isAuthorized then |
09 | isAuthorized = Instance.new( "BoolValue" , tempdata) |
10 | isAuthorized.Name = "IsAuthorized" |
11 | isAuthorized.Value = false |
16 | function togglePlayerAuth(player) |
17 | player:WaitForChild( "TempData" ) |
18 | local tempdata = player:FindFirstChild( "TempData" ) |
19 | local isAuthorized = player:FindFirstChild( "IsAuthorized" ) |
20 | if isAuthorized.Value = = false then |
21 | isAuthorized.Value = true |
23 | isAuthorized.Value = false |
27 | game.Workspace.YourLightswitch.MouseClick:connect( function (p) |
28 | local tempdata = p:FindFirstChild( "TempData" ) |
29 | local isAuthorized = tempdata:FindFirstChild( "IsAuthorized" ) |
30 | if isAuthorized.Value = = false then |
37 | game.Players.PlayerAdded:connect(onPlayerLogin) |
WARNING: Be aware when selecting from LocalScript or Script, if you use LocalScript then the code will execute on the CURRENT PLAYER RUNNING THE SCRIPT;
If you instead of the LocalScript you decide to use the Script (the normal one) then this code will run on EACH PLAYER CALLED FROM THE FUNCTION!