So i'm trying to make a spectator script that allows me only to spectate players in game. To do this, i'm using collectionservice to get all of the players with the specific tag, and make it into a table.
I try stuff like:
local tagged = collectionservice:GetTagged("alive") local players = {} local position = 0 for _,v in pairs(tagged) do table.insert(players, v) end specButton.MouseButton1Down:connect (function () position = position + 1 spectating = game.Players:FindFirstChild(players[position]) Camera.CameraSubject = spectating.Character.Humanoid end)
but "spectating" is always "nil". I think im doing something wrong with the table, but i don't know what. any help is appreciated :)
Most of your code is unnecessary, since the use of GetTagged is incorrect. Try this:
local players = collectionservice:GetTagged("alive") local position = 0 specButton.MouseButton1Down:connect (function () position = position + 1 Camera.CameraSubject = players[position].Character.Humanoid end)
(Note that GetTagged returns instances) Wiki Page