How You Make it Automatically Spectate Other Players When The Current One Is dead or Gone

I Having Trouble Putting This Feature
So basically i’m Trying to do is So That if The Player That You Spectating Currently has Died or Left The Game
it Switches To Another Random Person

This is the script


local debounce = false

local UserInputService = game:GetService("UserInputService")
local LocalPlayer = game:GetService("Players").LocalPlayer
local CurrentCamera = game.Workspace.CurrentCamera
local Deploy = script.Parent.ButtonFrame:WaitForChild("Play"):WaitForChild("ingame")
local SpectateMain = script.Parent:WaitForChild("SpectateMain")
local title = SpectateMain:WaitForChild("Title")
local Previous = SpectateMain:WaitForChild("Previous")
local Next = SpectateMain:WaitForChild("Next")
local Spectate = script.Parent:WaitForChild("Spectate")

function GetPlayers()
	for _,v in pairs(game.Players:GetPlayers())do
		if v.Name == title.Text then
			return(_)
		end
	end
end

Spectate.MouseButton1Click:connect(function()
	if debounce == false then
		debounce = true
		SpectateMain.Visible = true

		pcall(function()
			title.Text = game.Players:GetPlayerFromCharacter(CurrentCamera.CameraSubject.Parent).Name
		end)
	elseif debounce == true then
		debounce = false
		pcall(function()
			CurrentCamera.CameraSubject = game.Players.LocalPlayer.Character.Humanoid
		end)

		SpectateMain.Visible = false
	end
end)

Previous.MouseButton1Click:connect(function()
	local players = game.Players:GetPlayers()
	local Number = GetPlayers()
	if not pcall(function() 
			CurrentCamera.CameraSubject = players[Number-1].Character.Humanoid
			if players[#players].CharacterRemoving then
				CurrentCamera.CameraSubject = players[Number-1].Character.Humanoid
			end
		end)
	then
		CurrentCamera.CameraSubject = players[#players].Character.Humanoid
	end

	pcall(function()
		title.Text = game.Players:GetPlayerFromCharacter(CurrentCamera.CameraSubject.Parent).Name
	end)
end)

Next.MouseButton1Click:connect(function()
	local players = game.Players:GetPlayers()
	local Number = GetPlayers()
	if not pcall(function() 
			CurrentCamera.CameraSubject = players[Number+1].Character.Humanoid
			if players[#players].CharacterRemoving then
				CurrentCamera.CameraSubject = players[Number+1].Character.Humanoid
			end
		end) then
		CurrentCamera.CameraSubject = players[1].Character.Humanoid
	end

	pcall(function()
		title.Text = game.Players:GetPlayerFromCharacter(CurrentCamera.CameraSubject.Parent).Name
	end)
end)

Deploy.MouseButton1Click:connect(function()
	if debounce == true then
		debounce = false
		pcall(function()
			CurrentCamera.CameraSubject = game.Players.LocalPlayer.Character.Humanoid
		end)

		SpectateMain.Visible = false
	end
end)

UserInputService.InputBegan:connect(function(object, m)
	if not m then
		if object.UserInputType == Enum.UserInputType.Keyboard then
			local key = object.KeyCode
			if key == Enum.KeyCode.M then 
				if debounce == true then
					debounce = false
					pcall(function()
						CurrentCamera.CameraSubject = game.Players.LocalPlayer.Character.Humanoid
					end)

					SpectateMain.Visible = false
				end
			end
		end
	end
end)

game.ReplicatedStorage:FindFirstChild("StopSpectate").OnClientEvent:Connect(function()
	if debounce == true then
		debounce = false
		pcall(function()
			CurrentCamera.CameraSubject = game.Players.LocalPlayer.Character.Humanoid
		end)

		SpectateMain.Visible = false
	end
end)

UserInputService.InputBegan:connect(function(object, m)
	if not m then
		if object.UserInputType == Enum.UserInputType.Keyboard then
			local key = object.KeyCode
			if key == Enum.KeyCode.M then 
				if debounce == true then
					debounce = false
					pcall(function()
						CurrentCamera.CameraSubject = game.Players.LocalPlayer.Character.Humanoid
					end)

					SpectateMain.Visible = false
					LocalPlayer.CameraMaxZoomDistance = 50
					LocalPlayer.CameraMinZoomDistance = 10
				end
			end
		end
	end
end)

Monitor the player you’re spectating. When they die, though humanoid.Died, you can pick another random player and switch to spectating them.

2 Likes