Spectators Count

Hello,

I know this is a bit of a weird request, but I am working on a spectating script, and I want to make it so that when someone spectates you, the count goes up by one, and if the player that is spectating you switches or leaves then the count will go down by 1.

Here is the script:

local playerService = game:GetService("Players")
local camera = workspace.CurrentCamera

local folder = script.Parent

local spectateButton = folder.SpectateButton
local spectateFrame = folder.SpectateFrame
local upButton = spectateFrame.UpButton
local downButton = spectateFrame.DownButton
local playerText = spectateFrame.PlayerText.PlayerName

local countFrame = folder.CountFrame
local countText = countFrame.Count

local toggled = false

local function toggleButton()
	if toggled == false then
		toggled = true

		spectateFrame.Visible = not spectateFrame.Visible
		playerText.Text =  playerService.LocalPlayer.Name
	elseif toggled == true then
		toggled = false

		spectateFrame.Visible = not spectateFrame.Visible
		playerText.Text =  playerService.LocalPlayer.Name
		camera.CameraSubject = playerService.LocalPlayer.Character.Humanoid
	end
end

local function upButtonIsClicked()
	local selectedPlayer = playerService:GetPlayers()
	local maxIndex = #selectedPlayer

	index += 1

	if index > maxIndex then
		index = 1
	end

	local player = selectedPlayer[index]
	camera.CameraSubject = player.Character.Humanoid
	playerText.Text = player.Name
end

local function downButtonIsClicked()
	local selectedPlayer = playerService:GetPlayers()
	local maxIndex = #selectedPlayer

	index -= 1

	if index < 1 then
		index = maxIndex
	end

	local player = selectedPlayer[index]
	camera.CameraSubject = player.Character.Humanoid
	playerText.Text = player.Name
end

spectateButton.MouseButton1Click:Connect(toggleButton)
upButton.MouseButton1Click:Connect(upButtonIsClicked)
downButton.MouseButton1Click:Connect(downButtonIsClicked)

The best way to go would be to fire a remote event every time someone changes their spectating target, then firing all clients with information about who is spectating who.

1 Like