Rank Locked Camera Manipulation

Greetings, I am wondering if anyone would know how I could edit the script I have done so it makes players who are in group 5682942 and rank 23 which presses a button their camera would go to the part. When they click it again their camera would go back to normal.

Script:
local Workspace = game:GetService(“Workspace”)
local PlayerService = game:GetService(“Players”)
local StarterGui = game:GetService(“StarterGui”)

local PlayerCamera = Workspace.CurrentCamera
local Camera = Workspace:FindFirstChild(“Stage_Camera”)
local Player = PlayerService.LocalPlayer

local Button = script.Parent

local OutputUI = script.Parent.Parent.Parent.Black_Screen

Button.MouseButton1Click:Connect(function()

local Viewing = script:GetAttribute("Viewing")

if PlayerCamera then
	
	if Viewing == false then
		
		OutputUI:TweenPosition(UDim2.new(4,0,0,0), Enum.EasingDirection.Out, Enum.EasingStyle.Linear, 2)
		
		wait(1)
		
		PlayerCamera.CameraType = Enum.CameraType.Scriptable
		PlayerCamera.CameraSubject = Camera
		PlayerCamera.CFrame = Camera.CFrame
		
		StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.PlayerList, false)
		
		script:SetAttribute("Viewing", true)
		
	elseif Viewing == true then
				
		OutputUI:TweenPosition(UDim2.new(-4,0,0,0), Enum.EasingDirection.Out, Enum.EasingStyle.Linear, 2)

		wait(1)
		
		PlayerCamera.CameraType = Enum.CameraType.Custom
		PlayerCamera.CameraSubject = Player.Character.Humanoid
		PlayerCamera.CFrame = Player.Character.Head.CFrame
		
		StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.PlayerList, true)
		
		script:SetAttribute("Viewing", false)
		
	end
	
end

end)

Hey! So this is how I would personally approach this:


local GroupId = 5682942
local RequiredRank = 23

Button.MouseButton1Click:Connect(function()
    local Viewing = script:GetAttribute("Viewing")

    -- Check if the player is in the required group and has the required rank
    local PlayerGroup = Player:GetRoleInGroup(GroupId)
    if PlayerGroup ~= nil and PlayerGroup >= RequiredRank then
        if PlayerCamera then
            if Viewing == false then
                -- rest of the code remains the same
                ...
            elseif Viewing == true then
                -- rest of the code remains the same
                ...
            end
        end
    end
end)

Hiya! Thank you for your reply I have implented your code inside of my code and it seems to come up with this error.

Are you able to show me the entire block of code in the mousebutton1click?

Sorry for the late reply but it seems as I explained it wrong, I meant that if they click the button then everyone’s camera would transition to the part called “camera” when they click the button again it would go back to normal. I feel as I scripted it wrong