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)
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
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)