Hello, I am making a round script with a random map selector, and for some odd reason, my script is giving me an error when checking if there is one player in a table. Here's the script:

local InLobby = {}
local contestants = {}
local Status = game:GetService("ReplicatedStorage"):WaitForChild("Status")
local MapFol = game:GetService("ReplicatedStorage"):WaitForChild("Maps"):GetChildren()

game.Players.PlayerAdded:Connect(function(plr)
	table.insert(InLobby, plr)
end)

while true do
	local random = math.random(1, #MapFol)

	for i = 10, 0, -1 do

		Status.Value = "Intermission: "..i.." seconds left"

		if Status.Value == "Intermission: 0 seconds left" then
			for pos, player in pairs(InLobby) do
				if player then
					table.remove(InLobby, pos)
					table.insert(contestants, player)
					print(player.Name.." has been removed from InLobby table and added into the contestants table")
					break
				end
			end
		end
		wait(1)
	end

	if Status.Value == "Intermission: 0 seconds left" then

		for _, player in pairs(game.Players:GetPlayers()) do
			local GameTag = Instance.new("BoolValue", player)
			GameTag.Name = "GameTag"
		end

		for i = 15, 0, -1 do
			Status.Value = "Round in Progress: "..i.." seconds left"
			wait(1)
			for pos, plr in pairs(contestants) do
				local char = plr.Character
				local hum = char:FindFirstChild("Humanoid")
				local humRootPart = char:FindFirstChild("HumanoidRootPart")

				if char and plr and plr:FindFirstChild("GameTag") then
					if random == 1 then
						local MapsClone = game.ReplicatedStorage.Maps:Clone()
						humRootPart.CFrame = MapsClone.Part1.CFrame

					elseif random == 2 then
						local MapsClone = game.ReplicatedStorage.Maps
						humRootPart.CFrame = MapsClone.Part2.CFrame

					elseif random == 3 then
						local MapsClone = game.ReplicatedStorage.Maps:Clone()
						humRootPart.CFrame = MapsClone.Part3.CFrame
					end

					if not char then
						-- The Player left the game.
					end
				end
				
				if #plr == 1 then
					Status.Value = plr[1].Name.." has won the game!"
					plr[1].leaderstats.Money.Value = plr[1].leaderstats.Money.Value + math.random(50, 100)
				end
				
				if not plr:FindFirstChild("GameTag") then
					table.remove(contestants, pos)
					table.insert(InLobby, plr)
					print(plr.Name.." has been removed from the contestants table!")
				else
					print(plr.Name.." is still in the game!")
				end
				if #plr >= 2 and Status.Value == "Round In Progress: 0 seconds left" then
					for i, v in pairs(game.Workspace.Maps) do
						v:Destroy()
					end
				end
			end
		end
	end

	for _, plr in pairs(game.Players:GetPlayers()) do
		local hum = plr.Character:FindFirstChild("Humanoid")

		hum.Died:Connect(function()
			if plr:FindFirstChild("GameTag") then
				plr.GameTag:Destroy()
			end
		end)
	end
end

https://gyazo.com/3051243ab5c0eb351aff47a22f337b00

If you find an answer, please comment below!

2 comments

0

There is clearly something wrong, and it is quite obvious too. You're traversing through the 'contestants' array which is comprised of Player Objects (Instances), therefore 'plr' refers to the current Player Object respective to the current iteration. The length operator is therefore being used incorrectly. I assume you meant, '#contestants'.

0

Oh thank you, it worked now.