Why error occurs
1.Error occurs when player exits game
2.Error occurs when the button is repeatedly clicked
local gui = script.Parent
local Players = game:GetService(“Players”)
local player = game.Players.LocalPlayer
local cam = game.Workspace.CurrentCamera
local spectateB = gui:WaitForChild(“Spectate”)
local spectateW = gui:WaitForChild(“SpectateWindow”)
local main = spectateW:WaitForChild(“Main”)
local name = main:WaitForChild(“Name”)
local number = 1
spectateB.MouseButton1Down:connect(function()
if spectateW.Visible == false then
spectateW.Visible = true
spectateB.Text = “Stop Spectating”
for index, players in pairs(game.Players:GetPlayers()) do
if number == index then
local character = game.Workspace:WaitForChild(players.Name)
cam.CameraSubject = character:WaitForChild(“Head”)
name.Text = cam.CameraSubject.Parent.Name
end
end
cam.CameraType = “Follow”
else
spectateW.Visible = false
spectateB.Text = “Spectate”
cam.CameraSubject = player.Character:FindFirstChild(“Humanoid”)
cam.CameraType = “Custom”
end
end)
local left = main:WaitForChild(“Left”)
local right = main:WaitForChild(“Right”)
right.MouseButton1Down:connect(function()
if number == #game.Players:GetPlayers() then
number = 1
else
number = number + 1
end
for index, players in pairs(game.Players:GetPlayers()) do
if number == index then
local character = game.Workspace:WaitForChild(players.Name)
cam.CameraSubject = players.character.Head
name.Text = game.Players:GetPlayerFromCharacter(cam.CameraSubject.Parent).Name
end
end
end)
left.MouseButton1Down:connect(function()
if number == 1 then
number = #game.Players:GetPlayers()
else
number = number - 1
end
for index, players in pairs(game.Players:GetPlayers()) do
if number == index then
local character = game.Workspace:WaitForChild(players.Name)
cam.CameraSubject = players.character.Head
name.Text = game.Players:GetPlayerFromCharacter(cam.CameraSubject.Parent).Name
end
end
end)
When writing a scripting support post try and use the format so that it is easier to read for the people trying to help you
local gui = script.Parent
local Players = game:GetService(“Players”)
local player = game.Players.LocalPlayer
local cam = game.Workspace.CurrentCamera
local spectateB = gui:WaitForChild(“Spectate”)
local spectateW = gui:WaitForChild(“SpectateWindow”)
local main = spectateW:WaitForChild(“Main”)
local name = main:WaitForChild(“Name”)
local number = 1
spectateB.MouseButton1Down:connect(function()
if spectateW.Visible == false then
spectateW.Visible = true
spectateB.Text = “Stop Spectating”
for index, players in pairs(game.Players:GetPlayers()) do
if number == index then
local character = game.Workspace:WaitForChild(players.Name)
cam.CameraSubject = character:WaitForChild(“Head”)
name.Text = cam.CameraSubject.Parent.Name
end
end
cam.CameraType = “Follow”
else
spectateW.Visible = false
spectateB.Text = “Spectate”
cam.CameraSubject = player.Character:FindFirstChild(“Humanoid”)
cam.CameraType = “Custom”
end
end)
local left = main:WaitForChild(“Left”)
local right = main:WaitForChild(“Right”)
right.MouseButton1Down:connect(function()
if number == #game.Players:GetPlayers() then
number = 1
else
number = number + 1
end
for index, players in pairs(game.Players:GetPlayers()) do
if number == index then
local character = game.Workspace:WaitForChild(players.Name)
cam.CameraSubject = players.character.Head
name.Text = game.Players:GetPlayerFromCharacter(cam.CameraSubject.Parent).Name
end
end
end)
left.MouseButton1Down:connect(function()
if number == 1 then
number = #game.Players:GetPlayers()
else
number = number - 1
end
for index, players in pairs(game.Players:GetPlayers()) do
if number == index then
local character = game.Workspace:WaitForChild(players.Name)
cam.CameraSubject = players.character.Head
name.Text = game.Players:GetPlayerFromCharacter(cam.CameraSubject.Parent).Name
end
end
end)
Could you tell us which errors specifically? And what scenarios are occurring when those errors occur? For example, the error that supposedly happens when the player exits the game, is it when your spectating the person who left? Or is it someone random? Or perhaps some other scenario?
That doesn’t really help at all. Send a screenshot of the error message in the output tab. And can you elaborate more on the scenario on which it occurs? Give us every detail you can possibly think of.
local gui = script.Parent
local Players = game:GetService("Players")
local player = game.Players.LocalPlayer
local cam = workspace.CurrentCamera
local spectateB = gui:WaitForChild("Spectate")
local spectateW = gui:WaitForChild("SpectateWindow")
local left = main:WaitForChild("Left")
local right = main:WaitForChild("Right")
local main = spectateW:WaitForChild("Main")
local name = main:WaitForChild("Name")
local number = 1
local function setSubject()
local plr = Players:GetPlayers()[number]
local char = plr and plr.Character
local head = char and char:FindFirstChild("Head")
if not head then return end
cam.CameraSubject = head
name.Text = char.Name
end
spectateB.MouseButton1Down:Connect(function()
if not spectateW.Visible then
spectateW.Visible = true
spectateB.Text = "Stop Spectating"
setSubject()
cam.CameraType = "Follow"
else
spectateW.Visible = false
spectateB.Text = "Spectate"
cam.CameraSubject = player.Character:FindFirstChild("Humanoid")
cam.CameraType = "Custom"
end
end)
right.MouseButton1Down:Connect(function()
if number == #game.Players:GetPlayers() then
number = 1
else
number += 1
end
setSubject()
end)
left.MouseButton1Down:Connect(function()
if number == 1 then
number = #game.Players:GetPlayers()
else
number -= 1
end
setSubject()
end)
You had a bit of repetition so I condensed it a bit with a function instead, also you can jsut reference the index directly instead of using a loop.
Your main issue is that at some points, you weren’t waiting for the head to exist, so I fixed that by making it check if there’s a head in your character if the Character was found, and if the head wasn’t found, do nothing