Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why does this error "Argument 1 missing or nil"?

Can anyone tell me what I'm doing wrong with this Spectate GUI as to why it's erroring on line 29? Error output is giving is Argument 1 missing or nil | Players.Player1.PlayerGui.ScreenGui.TextButton.LocalScript', Line 29.

Code(in a LocalScript):

-- Variables
local Player = game.Players.LocalPlayer
local Players = game.Players:GetPlayers()

local Alive = {}

-- Functions
local function GatherAlive()
	for i = 1,#Alive do
		if #Alive > 0 then
			print("Removed "..Alive[1])
			table.remove(Alive,1)
		end
	end
	for i = 1,#Players do
		if Players[i].TeamColor == "Bright red" or Players[i].TeamColor == "Bright blue" then
			table.insert(Alive,Players[i].Name)
			print("Added "..Alive[1])
		end
	end
end

script.Parent.MouseButton1Click:connect(function()
	GatherAlive()
	if #Alive > 0 then
		warn("0 alive")
		return false
	end
	game.Workspace.CurrentCamera.CameraSubject = game.Players:FindFirstChild(Alive[1]).Character.Humanoid
end)

Any help appreciated.

1 answer

✓ Accepted answer

Your if logic is wrong

script.Parent.MouseButton1Click:connect(function()
    GatherAlive()
    if #Alive == 0 then -- eq
        warn("0 alive")
        return false
    end
    game.Workspace.CurrentCamera.CameraSubject = game.Players:FindFirstChild(Alive[1]).Character.Humanoid
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.