I made a spectating script and it actually works fully but doesn’t work for the purpose that it was made for the script gives you an error “attempt to index nil with character” when you die in game or join late while game is on
Script
local Player = game.Players.LocalPlayer
local NextButtion = Player.PlayerGui.SpectatingUI.Frame:WaitForChild("Next")
local ProviousButtion = Player.PlayerGui.SpectatingUI.Frame:WaitForChild("Previous")
local GameInProgress = game.ReplicatedStorage:WaitForChild("GameInProgress_Repli")
local Camera = workspace.CurrentCamera
local Humanoid = Player.Character:WaitForChild("Humanoid")
local CloseButton = Player.PlayerGui.SpectatingUI:WaitForChild("ImageButton")
local SpectatingText = Player.PlayerGui.SpectatingUI.Frame:WaitForChild("TextLabel")
local OpenSpectateButtion = Player.PlayerGui.MenuButtons:WaitForChild("SpectatingButton")
local AvailableToSpectate = {}
NextButtion.MouseButton1Click:Connect(function()
if GameInProgress.Value == true then
print("yes5")
ChoosePlayerToSpecate(AvailableToSpectate)
local CharacterofChoosen = ChoosePlayerToSpecate(AvailableToSpectate)
Camera.CameraSubject = CharacterofChoosen.Character.Humanoid
SpectatingText.Text = CharacterofChoosen.Name
end
end)
CloseButton.MouseButton1Click:Connect(function()
Camera.CameraSubject = Player.Character.Humanoid
print("yes4")
end)
OpenSpectateButtion.MouseButton1Click:Connect(function()
SpectatingText.Text = Player.Name
Camera.CameraSubject = Player.Character.Humanoid
end)
ProviousButtion.MouseButton1Click:Connect(function()
if GameInProgress.Value == true then
print("yes5")
ChoosePlayerToSpecate(AvailableToSpectate)
local CharacterofChoosen = ChoosePlayerToSpecate(AvailableToSpectate)
Camera.CameraSubject = CharacterofChoosen.Character.Humanoid
SpectatingText.Text = CharacterofChoosen.Name
end
end)
Humanoid.Died:Connect(function()
task.wait(0.2)
if table.find(AvailableToSpectate,Player) and not Player:FindFirstChild("Student") and not Player:FindFirstChild("Baldi") then
for i, v in pairs(AvailableToSpectate) do
if v == Player then
print("it worked!")
table.remove(AvailableToSpectate,i)
end
end
end
end)
Humanoid.Died:Connect(function()
print(AvailableToSpectate)
end)
function ChoosePlayerToSpecate(AvailableToSpectate)
if GameInProgress.Value == true then
print("yes2")
local RandomObj = Random.new()
local ChoosenPlayerToSpectate = AvailableToSpectate[RandomObj:NextInteger(1,#AvailableToSpectate)]
return ChoosenPlayerToSpectate
end
end
GameInProgress:GetPropertyChangedSignal("Value"):Connect(function()
if GameInProgress.Value == true then
for i, v in pairs(game.Players:GetPlayers()) do
if v:FindFirstChild("Student") then
table.insert(AvailableToSpectate,v)
elseif v:FindFirstChild("Baldi") then
table.insert(AvailableToSpectate,v)
end
print("yes1")
end
else
AvailableToSpectate = {}
end
end)
if you need me to clarify more thing then tell I will try my best to answer!
If you are also wondering that type of script this is its a local script