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

Output says I have a Bad Argument?

Here is my code: You don't have to read all of it, just read from line 139 and on. Line 139 was given an error (bad argument). You'll see the output under the code. Help?

Code:

script.Parent.Disabled = true

local playerstorage = game:GetService("Players")
local storage = game:GetService("ServerStorage")
local guifolder = storage.Guis
local players = {}
local holder = game.Workspace:WaitForChild("MapHolder")
local maps = game.Lighting:WaitForChild("Maps")
local message = game.Lighting.Message
local roundtime = 1 * 5

	
for _, player in pairs(game.Players:GetPlayers()) do
  local humanoid = player.Character:WaitForChild("Humanoid")
  if humanoid and humanoid.Health > 0 then
    table.insert(players, player)
  end
end



message.Value = "Map Being Chosen..."
	
holder:ClearAllChildren()
wait(2)	

local allmaps = maps:GetChildren()
local newmap = allmaps[math.random(1, #allmaps)]
newmap:Clone().Parent = holder
print 'Map put!'
wait(2)

-- Tell players the map!


if (holder:FindFirstChild("Warehouse")) then
	
	message.Value = "Map Chosen: Space Warehouse"
	wait(5)
end

	-- Alien Only!
  local alien = table.remove(players, math.random(#players))
  local alieng = guifolder.Alien
  local alieng2 = alieng:Clone()
  alieng2.Parent = alien.PlayerGui

  for _, player in pairs(players) do
	-- only into non-aliens!
    print(player)
    guifolder.Soldier:Clone().Parent = player.PlayerGui
  end

-- TP the players!


for _, player in pairs(game.Players:GetPlayers()) do
  local humanoid = player.Character:WaitForChild("Humanoid")
  if humanoid and humanoid.Health > 0 then
    table.insert(players, player)
  end
end


local spawnmodel =  newmap:WaitForChild("Spawns")
local spawns = spawnmodel:GetChildren()
for _, player in pairs(players) do
	if player and player.Character and #spawns > 0 then
		local Torso = player.Character:WaitForChild("Torso")
		local spawnindex = math.random(1, #spawns)
		local spawn = spawns[spawnindex]
		if spawn and Torso then
			table.remove(spawns, spawnindex)
			Torso.CFrame = CFrame.new(spawn.Position + Vector3.new(0, 3, 0))
			
				local activetag = Instance.new("StringValue")
				activetag.Name = "activetag"
				activetag.Parent = player.Character			
			
		end
	end
end

message.Value = "Round In Progress"


-- TIMER

local localtimer = roundtime
while localtimer > 0 do		
	wait(1)
	localtimer = localtimer - 1
	message.Value = "Round Time Left: (" .. localtimer .. ")"
	
	activeplayers = {}
	alienactive = false
	
	local player = players[math.random(1, #players)]
	
	for _, players in pairs(players) do
		
		if player then			
			local character = player.Character
			if character then
				local activetag = character:FindFirstChild("activetag")
				local humanoid = character:FindFirstChild("Humanoid")
				if activetag and humanoid and humanoid.Health > 0 then
					alienactive = true
					end
					table.insert(activeplayers, player)
					break
				end
			end
		if #activeplayers <=1 or not alienactive then
			break
		end
	end
	
end

local gameresults = "The Alien Lost!"

if alienactive then
	 if #players > 2 then
		--alien failed!
		message.Value = "The Alien Lost!"
	else
		gameresult = "The Alien Has Killed Everybody!"
		message.Value = "The Alien Has Killed Everybody!"
	end
else
	--Alien Captured
end

--TP THEM BACK!!!

local lobbyspawns = {}
local lobby = game.Workspace.Lobby
 for _, v in pairs(lobby) do
	if v and v.Name == "spawn" then
		table.insert(lobbyspawns, v)
	end
end

for _, player in pairs(activeplayers) do
	if player then
		if player.Character then
			local humanoid = player.Character.Humanoid
			if humanoid then
				humanoid:UnequipTools()
			end
		end
		local randomspawn = lobbyspawns[math.random(1, #lobbyspawns)]
		player.Character:MoveTo(randomspawn.Position)
		local backpack = player:FindFirstChild("Backpack")
		if backpack then
			backpack:ClearAllChildren()
		end
	end
end
	
script.Parent.Disabled = false
script.Disabled = true



Output:

11:24:16.724 - ServerScriptService.Game.G:139: bad argument #1 to 'pairs' (table expected, got Object)

1 answer

✓ Accepted answer

The output tells you the exact problem. You can't loop through an object, you can only loop through a list. Therefore you need to use GetChildren(), because it returns a list of the object's children.

for _, v in pairs(lobby:GetChildren()) do

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