✓ Accepted answer
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:
local player = game.Players.LocalPlayer
local isAuthorized = false
function togglePlayerAuth()
if not isAuthorized then
isAuthorized = true
else
isAuthorized = false
end
end
game.Workspace.YourLightswitch.MouseClick:connect(function(p)
if not isAuthorized then
else
end
end)
Script Way
You can declare a BoolValue called "isAuthorized" this too, same way of the LocalScript way.
function onPlayerLogin(p)
local tempdata = p:FindFirstChild("TempData")
if not tempdata then
tempdata = Instance.new("Model", p)
tempdata.Name = "TempData"
else
local isAuthorized = tempdata:FindFirstChild("IsAuthorized")
if not isAuthorized then
isAuthorized = Instance.new("BoolValue", tempdata)
isAuthorized.Name = "IsAuthorized"
isAuthorized.Value = false
end
end
end
function togglePlayerAuth(player)
player:WaitForChild("TempData")
local tempdata = player:FindFirstChild("TempData")
local isAuthorized = player:FindFirstChild("IsAuthorized")
if isAuthorized.Value == false then
isAuthorized.Value = true
else
isAuthorized.Value = false
end
end
game.Workspace.YourLightswitch.MouseClick:connect(function(p)
local tempdata = p:FindFirstChild("TempData")
local isAuthorized = tempdata:FindFirstChild("IsAuthorized")
if isAuthorized.Value == false then
else
end
end
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!