Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Spectator script using collectionservice?

Asked by 6 years ago

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

Log in to vote
0
Answered by
xAtom_ik 574 Moderation Voter
6 years ago

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

Ad

Answer this question