How do you make a spectate script that changes players when the player that you spectating, dies

so i have this script that spectate players but there’s a problem with it, so if the player that you spectating on, died it just locks the camera there and i wanted to make the script changes player when the player that you spectating on, dies

heres the script

cam = game.Workspace.CurrentCamera

local bar = script.Parent.Bar
local title = bar.Title
local prev = bar.Previous
local nex = bar.Next
local button = script.Parent.Spectate
local players = game.Players:GetPlayers()
local num = get()

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


local debounce = false
button.MouseButton1Click:connect(function()
	if debounce == false then debounce = true
		bar.Visible = true
		pcall(function()
			title.Text = game.Players:GetPlayerFromCharacter(cam.CameraSubject.Parent).Name
		end)
	elseif debounce == true then debounce = false
		pcall(function() cam.CameraSubject = game.Players.LocalPlayer.Character.Humanoid end)
		bar.Visible = false
	end
end)

prev.MouseButton1Click:connect(function()
	wait(.1)
	if not pcall(function() 
			cam.CameraSubject = players[num-1].Character.Humanoid
		end) then
		cam.CameraSubject = players[#players].Character.Humanoid
	end
	pcall(function()
		title.Text = game.Players:GetPlayerFromCharacter(cam.CameraSubject.Parent).Name
	end)
end)

nex.MouseButton1Click:connect(function()
	wait(.1)
	if not pcall(function() 
			cam.CameraSubject = players[num+1].Character.Humanoid
		end) then
		cam.CameraSubject = players[1].Character.Humanoid
	end
	pcall(function()
		title.Text = game.Players:GetPlayerFromCharacter(cam.CameraSubject.Parent).Name
	end)
end)


You can refer.Died event of whatever player’s humanoid you’re looking at so then when the player dies, you force change to spectate the next player.

1 Like

oh, well could you give a example of the .died script its new to for me

local Players = game:GetService("Players")

local Player = Players.LocalPlayer
local cam = workspace.CurrentCamera

local bar = script.Parent.Bar
local title = bar.Title
local prev = bar.Previous
local nex = bar.Next
local button = script.Parent.Spectate

--gets the players once, the script should run every time after a round starts.
local players = Players:GetPlayers()

function get(t) 
	for i, v in pairs(players) do 
		if v.Name == t then 
			return i 
		end 
	end
end

--made it a single function to remove duplicates
function UpdateTitle()
	pcall(function()
		title.Text = Players:GetPlayerFromCharacter(cam.CameraSubject.Parent).Name
	end)
end

--if unable to find player from title.Text
--priority: player in textbox, local player, first player in table
local num = get(title.Text) or table.find(players, Player) or 0

local debounce = false
button.Activated:Connect(function()
	if not debounce then 
		UpdateTitle()
	else
		pcall(function() cam.CameraSubject = Player.Character.Humanoid end)
	end
	bar.Visible = debounce 
	debounce = not debounce 
end)

--made the button events a single function cause they where simillar
function changeSpectator(value)
	local n, n2 = 1, 1
	if not value then 
		n, n2 = -1, #players 
	end
	task.wait(.1)
	local Success, Error = pcall(function()
		cam.CameraSubject = players[num+n].Character.Humanoid 
	end)
	if not Success then 
		cam.CameraSubject = players[n2].Character.Humanoid
	end
	UpdateTitle()
end

prev.Activated:Connect(function()
	changeSpectator(false) 
end)

nex.Activated:Connect(function()
	changeSpectator(true)
end)

--check if players are alive, if they are not remove them from the table.
for i, player in pairs(players) do 
	local char = player.Character 
	if not char then table.remove(players, i) return end 
	local humanoid = char:FindFirstChildWhichIsA("Humanoid")
	if not humanoid then table.remove(players, i) return end 
	local connection
	connection = humanoid.Died:Connect(function()
		table.remove(players, i)
		connection:Disconnect()
	end)
end
2 Likes

https://developer.roblox.com/en-us/api-reference/event/Humanoid/Died

.Died isn’t a script it’s an event shared by Humanoid instances.

1 Like

there’s something wrong with the code could you test it if it works properly

Do you run the script in a LocalScript after the round starts?

i did but the arrow buttons didn’t work, also i think the local debounce should be local debounce = true

local cam = game.Workspace.CurrentCamera

local bar = script.Parent.Bar
local title = bar.Title
local prev = bar.Previous
local nex = bar.Next
local button = script.Parent.Spectate
local players = game.Players:GetPlayers()
local num = get()

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


local debounce = false
button.MouseButton1Click:connect(function()
	if debounce == false then debounce = true
		bar.Visible = true
		pcall(function()
			title.Text = game.Players:GetPlayerFromCharacter(cam.CameraSubject.Parent).Name
		end)
	elseif debounce == true then debounce = false
		pcall(function() cam.CameraSubject = game.Players.LocalPlayer.Character.Humanoid end)
		bar.Visible = false
	end
end)

prev.MouseButton1Click:connect(function()
	wait(.1)
	if not pcall(function() 
			cam.CameraSubject = players[num-1].Character.Humanoid
		end) then
		cam.CameraSubject = players[#players].Character.Humanoid
	end
	pcall(function()
		title.Text = game.Players:GetPlayerFromCharacter(cam.CameraSubject.Parent).Name
	end)
end)

nex.MouseButton1Click:connect(function()
	wait(.1)
	if not pcall(function() 
			cam.CameraSubject = players[num+1].Character.Humanoid
		end) then
		cam.CameraSubject = players[1].Character.Humanoid
	end
	pcall(function()
		title.Text = game.Players:GetPlayerFromCharacter(cam.CameraSubject.Parent).Name
	end)
end)

players[num].Character.Humanoid.Died:Connect(function()
	if players[num] ~= nil then
		cam.CameraSubject = players[num+1].Character.Humanoid
	else
		players[num] = 0
		cam.CameraSubject = players[num+1].Character.Humanoid
	end
end)

Hope it works! :slight_smile:

so i got a error it said “Players.Player1.PlayerGui.Screen.SpectateHandler:9: attempt to call a nil value”

local num = get()

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

The function was assigned to the variable before the function was defined, I believe he meant.

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

local num = get()

I haven’t checked the rest of the code but if you have any further issues let me know.

1 Like

it still doesn’t work could you test it out?