How do I make spectate only for lobby?

Hey, I have a spectate script with me and what it does is it lets people spectate. Problem is the killer can spectate and the runners can spectate too. How do I make it only for lobby.

Game is round based

local player = game.Players.LocalPlayer
local team
local camera = game.Workspace.CurrentCamera 

local spectate = script.Parent:WaitForChild("Spectate")

local contestants = {}
local current = 1

local maxContestants = 0

game:GetService("RunService").RenderStepped:Connect(function()
    
    contestants = {}
    
    for i,v in pairs(game.Players:GetChildren()) do
        table.insert(contestants,v)
    end
    
    camera.CameraType = Enum.CameraType.Follow
    
    if #contestants <= maxContestants then
        spectate.SpectateFrame.Visible = false
        camera.CameraSubject = player.Character.Humanoid
    end
        
    if spectate.SpectateFrame.Visible == true then

        local playerSpectating = contestants[current]
        if not playerSpectating then playerSpectating = contestants[1] end

        local char = playerSpectating.Character

        if char and char:FindFirstChild("Humanoid") then camera.CameraSubject = char:FindFirstChild("Humanoid") end

        spectate.SpectateFrame.Current.Text = playerSpectating.Name
        
    elseif spectate.SpectateFrame.Visible == false then 
        camera.CameraSubject = player.Character.Humanoid 
    end
    
end)

spectate.SpectateButton.MouseButton1Click:Connect(function()
    
    if #contestants <= maxContestants then return end
    
    spectate.SpectateFrame.Visible = not spectate.SpectateFrame.Visible
    
end)

spectate.SpectateFrame.Back.MouseButton1Click:Connect(function()

    if current == 1 then
        current = #contestants
    else
        current -= 1
    end

end)

spectate.SpectateFrame.Fwd.MouseButton1Click:Connect(function()

    if current == #contestants then
        current = 1
    else
        current += 1
    end

end)

This might not be the best possible solution but you can add a huge part around the lobby and detect if the player is inside that part. If the player is inside it then the GUI that you are supposed to press to spectate is disabled otherwise it’s enabled.
To check if player is in lobby:

local part = game.workspace.PlaceHolder.region -- the region in which players are to be detected place it around the lobby and turn cancollide off
	part.Touched:Connect(function() end) --Just for touch interest

	local plyrs = game:GetService("Players"):GetChildren()
	
	local function CheckIfInArea(Part, Character) -- function that checks if player is in certain area
		local touching = Part:GetTouchingParts() --gets all the parts touching the part
		for i = 1, #touching do 
			if touching[i] == Character.HumanoidRootPart then --if player is touching the part returns true else its false
				return true
			end
		end
		return false
	end

	for i, v in pairs (plyrs) do
		local char = v.Character
		local playerInArea = CheckIfInArea(part, char)
		if playerInArea == false then --if the player isnt in lobby then disable gui
            print(v.Name.. " is not in lobby")
			-- disable the GUI here
		else
			print(v.Name.. " is already in lobby")
            --enable the GUI from the player here
		end
	end

Or if the game uses teams they can disable the UI for certain teams

2 Likes

Region3 can also be used and it would result in fewer lines of code but I don’t know how it works so yes.
No region3

1 Like