Argument 1 missing or nil error

Hi, I’m trying to make a spectate system. I don’t see why this says argument one missing or nil.

Code:

local debounce = true
local tweenservice = game:GetService("TweenService")
local playerGui = game.Players.LocalPlayer.PlayerGui

local lastButton = script.Parent.Parent.SpectateFrame.Prev
local nextButton = script.Parent.Parent.SpectateFrame.Next
local playerUser = nextButton.Parent.TextLabel

local cam = workspace.CurrentCamera

local char = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")

local isSpectating = false
local currentIndex = 1
local playerList = {}

local function updatePlayers()
	local newPlayerList = {}
	
	for plrs, plr in pairs(game.Players:GetPlayers()) do
		if plr ~= game.Players.LocalPlayer then
			table.insert(newPlayerList, plr.Name)
		end
	end
end

local function updateSpectator()
	local foundPlayer = game.Players:FindFirstChild(playerList[currentIndex]) --This is the line that errored
	
	if foundPlayer then
		playerUser.Text = foundPlayer.Name
		cam.CameraSubject = foundPlayer.Character.Head
	end
end

local function nextPlayer()
	if currentIndex > #game.Players:GetPlayers() then
		currentIndex += 1
	else
		currentIndex = 1
	end
	updateSpectator()
end

local function lastPlayer()
	if currentIndex > 1 then
		currentIndex -= 1
	else
		currentIndex = #playerList
	end	
	updateSpectator()
end

local function closeSpectate()
	cam.CameraSubject = hum
	currentIndex = 1
end

script.Parent.MouseButton1Click:Connect(function()
	local goal,goal2,goal3 = {},{},{}
	goal.Size = 0
	goal2.Position = UDim2.new(0.321, 0,1.424, 0)
	goal3.Position = UDim2.new(0.182, 0,0.654, 0)
	local tweenInfo = TweenInfo.new(1,Enum.EasingStyle.Sine)
	local tw2 = TweenInfo.new(7,Enum.EasingStyle.Exponential)
	local light = game.Lighting.Blur
	tweenservice:Create(light,tweenInfo,goal):Play()
	tweenservice:Create(script.Parent,tw2,goal2):Play()
	tweenservice:Create(script.Parent.Parent.Credits,tw2,goal2):Play()
	tweenservice:Create(script.Parent.Parent.Play,tw2,goal2):Play()
	tweenservice:Create(script.Parent.Parent.SpectateFrame,tweenInfo,goal3):Play()
	playerGui.MenuCameraScript.Enabled = false	
	isSpectating = not isSpectating
	if isSpectating  then
		updatePlayers()
		updateSpectator()
	else
		closeSpectate()
	end
end)

nextButton.MouseButton1Click:Connect(nextPlayer)
lastButton.MouseButton1Click:Connect(lastPlayer)

Your updatePlayers function is not updating your playerList table and therefore the table is left empty leading to a nil index.

Here is how I would approach the updatePlayers function:

local function updatePlayers()
    table.clear(playerList);
    for plrs, plr in pairs(game.Players:GetPlayers()) do
		if plr ~= game.Players.LocalPlayer then
			table.insert(playerList, plr.Name);
		end
    end
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.