I have a spectate gui that is supposed to cycle through only players that have a value inside of them set to true, using a for loop to do this. Here's the script:

function clicked()
	local players = game.Players:GetChildren()
	local num = script.Parent.Parent.p
	local camera = game.Workspace.CurrentCamera
	local characterImageFormat='http://www.roblox.com/Thumbs/Avatar.ashx?x=352&y=352&format=png&userid=%d';
	if players[2] == nil then
		local spgui = game.Players.LocalPlayer.PlayerGui.spectategui.Frame
		local players = game.Players:GetChildren()
		local camera = game.Workspace.CurrentCamera
		camera.CameraSubject = game.Players.LocalPlayer.Character.Humanoid
		spgui.ImageLabel.Image = ""
		spgui.t.Text = "No players in the server."
		return
	end
	for a, players in ipairs(players) do
		if players.IsPlaying.Value == true then
			num.Value = num.Value + 1
			if players[num.Value] == nil then -- This is where the script breaks
				num.Value = 1
				if players[num.Value].Name == game.Players.LocalPlayer.Name then
					num.Value = num.Value + 1
					camera.CameraSubject = players[num.Value].Character.Humanoid
					script.Parent.Parent.ImageLabel.Image = 
					characterImageFormat:format(players[num.Value].userId)
					script.Parent.Parent.t.Text = players[num.Value].Name
				else
					camera.CameraSubject = players[num.Value].Character.Humanoid
					script.Parent.Parent.ImageLabel.Image = 
					characterImageFormat:format(players[num.Value].userId)
					script.Parent.Parent.t.Text = players[num.Value].Name
				end
			else
				if players[num.Value].Name == game.Players.LocalPlayer.Name then
					num.Value = num.Value + 1
					if players[num.Value] == nil then
						num.Value = 1
						if players[num.Value].Name == game.Players.LocalPlayer.Name then
							num.Value = num.Value + 1
							camera.CameraSubject = players[num.Value].Character.Humanoid
							script.Parent.Parent.ImageLabel.Image = 
							characterImageFormat:format(players[num.Value].userId)
							script.Parent.Parent.t.Text = players[num.Value].Name
						else
							camera.CameraSubject = players[num.Value].Character.Humanoid
							script.Parent.Parent.ImageLabel.Image = 
							characterImageFormat:format(players[num.Value].userId)
							script.Parent.Parent.t.Text = players[num.Value].Name
						end
					end
				else
					camera.CameraSubject = players[num.Value].Character.Humanoid
					script.Parent.Parent.ImageLabel.Image = 
					characterImageFormat:format(players[num.Value].userId)
					script.Parent.Parent.t.Text = players[num.Value].Name
				end
			end
		end
	end
end

script.Parent.MouseButton1Down:connect(clicked)

The problem is that later on in the script, I want to target specific players inside a table (i.e. players[6]), but it instead treats it as a property, making the script break. I need to find out how to shorten a table inside a for loop AND keep it as a table so that using something like players[3] wouldn't break the script.

6 comments

0

If you mean you want to remove the character from the table without destroying the player, you can set it to nil or use table.remove()

0

I have no idea what you are talking about

0

I'm sorry about it being very vague but it's not an easy question to elaborate on. I'll try to make it easier by posting my whole script that is a spectate gui.

0

You need to rename the variable "players" that is in the for loop. Maybe to just "player"

0

It just does the same thing :/

0

Well, do change the variable to 'player' and the table as 'players'. This is a simple syntax error. As for what you are trying to do, I have no idea.