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

Spectator script using collectionservice?

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 :)

1 answer

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

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