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

how would you fix this playertable for a spectate gui?

okay so i'm trying to make a spectate gui that only spectates a certain team. so i made a table to retrieve the players in that team.(this is localscript btw)

local clicked = false
local playernumber = 1
function getshooters()
	shooters = {}
	for i,v in pairs(game.Players:GetPlayers()) do
		if v.Team == game.Teams.Shooters then
			table.insert(shooters, v)
		end
		print(i,v)
	end
	print(#shooters)
end

function startSpectate()
if clicked == false then
	clicked = true
	for i, players in pairs(shooters) do
		if i == playernumber then
			local chosenplayer = game.Workspace:WaitForChild(players.Name)
			playercamera.CameraSubject = chosenplayer.Humanoid
			title.Text = players.Name
		end
	else
	clicked = false
	end
end
script.Parent.MouseButton1Click:connect(startSpectate)

function keypress(key)
	if clicked then
		getshooters()
		if key == "q" then
			if playernumber == 1 then
				playernumber = #shooters
			else
			playernumber = playernumber - 1
			end
			for i, players in pairs(shooters) do
				if index == playernumber then
					local chosenplayer = game.Workspace:WaitForChild(players.Name)
					playercamera.CameraSubject = chosenplayer.Humanoid
					title.Text = players.Name
				end
			end
		end
		if key == "e" then
			if playernumber == #shooters then
				playernumber = 1
			else
			playernumber = playernumber + 1
			end
			for index, players in pairs(shooters) do
				if index == playernumber then
					local chosenplayer = game.Workspace:WaitForChild(players.Name)
					playercamera.CameraSubject = chosenplayer.Humanoid
					title.Text = players.Name
				end
			end
		end
	end
end
mouse.KeyDown:connect(keypress)

(i added print(i,v) and print(#shooters) to see where i went wrong.)

okay so when I run this function on a local server with 3 players started in studio it starts to act weird. -with player1, with print(i,v) it works fine and prints out all the players on team shooter, but when I print(#shooters) it says that there are only 1 shooter, when there are 3. -with player2, print(i,v) still works out fine, but with #shooters it prints 2 when there are 3. -with player3, everything is working fine.

is there a problem with the script, or the loading of players, because the table is loading out fine, yet the #shooters is loading out wrong which messes with everything else.

1 comment

0

print(i,v) is different from the #shooters because it's outside the if statement. You should also be returning the table each time you need it, not setting it globally.

1 answer

Taking what Azarth said into consideration, your problem is likely that your players aren't being assigned to the right team properly.

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