So, I am trying to create a spectator handler for my game, Life and Death, and keep getting this error. What's wrong?
Here's the code,
-- Events
local spectate = game.ReplicatedStorage:WaitForChild("Events"):WaitForChild("PlayerSpectate")
-- Common variables
local players = game.Players:GetChildren()
-- Team variables
local survivors = BrickColor.new("White")
local zombies = BrickColor.new("Bright green")
game.ReplicatedStorage.Events.PlayerSpectate.OnServerEvent:connect(function(player, camera)
wait(1)
if player.Character ~= nil then
player.Character:Destroy()
player.Character = nil
local randomperson = nil
for i, v in pairs(players) do
if game.Workspace:FindFirstChild(v.Name) then
randomperson = game.Workspace:FindFirstChild(v.Name)
end
end
wait() -- Make sure our camera does not glitch, meh
camera.CameraSubject = randomperson.Humanoid
end
end)
Here's the error,
17:58:54.464 - ServerScriptService.SpectateHandler:26: attempt to index local 'randomperson' (a nil value)
17:58:54.467 - Stack Begin
17:58:54.471 - Script 'ServerScriptService.SpectateHandler', Line 26
17:58:54.473 - Stack End
