local SpecList = {}
game.Players.ChildAdded:Connect(function()
local Players = game.Players:GetPlayers()
print(Players)
print("Outside")
for i, v in ipairs(Players) do
print(SpecList)
local Value = v.Character:WaitForChild("Value").Value
local position = table.find(SpecList, v)
if Value ~= 2 and v.Name ~= Player.Name and position == nil then
table.insert(SpecList, v)
end
end
end)
A new issue has arose where nothing is being added to the array.
I don’t really know about the Value thing. if its value doesn’t change (so if it was 0 once it will always be 0), then you could do this:
local SpecList = {}
game.Players.ChildAdded:Connect(function(newPlr)
if newPlr:IsA("Player") then
local newChr = newPlr.Character or newPlr.CharacterAdded:Wait()
local Value = newChr:WaitForChild("Value").Value
if Value ~= 2 and newPlr.Name ~= Player.Name then
table.insert(SpecList, newPlr)
end
end
end)
I cleaned up your code a little bit, see if this works now.
local SpecList = {}
game.Players:PlayerAdded:Connect(function()
local Players = game.Players:GetPlayers()
print(#Players)
print("Outside")
for i, v in pairs(Players) do
print(SpecList)
if v then
local Value = v.Character:WaitForChild("Value").Value
local position = table.find(SpecList, v)
if Value ~= 2 and v.Name ~= Player.Name and position == nil then
table.insert(SpecList, v)
end
end
end
end)
local SpecList = {}
game:GetService("RunService").Heartbeat:Connect(function()
for _,plr in game.Players:GetPlayers() do
local char = plr.Character or plr.CharacterAdded:Wait()
local Value = char:WaitForChild("Value").Value
if Value ~= 2 and plr.Name ~= Player.Name and not table.find(SpecList,plr) then
table.insert(SpecList, plr)
end
end
end)
The easiest fix is to clear the table before re-populating it, or build a new table each time you try to switch perspectives. You can take inspiration from my more advanced spectation system: https://github.com/Ziffixture/Roblox/blob/main/Spectate.lua
It should take very little code to modify the getTrackedCharactersInWorkspace function to reject characters who are currently spectating
I figured out how I can do what I wanted without exploding the memory I don’t think. I split the loop into 2 parts. Here’s what it looks like now:
local SpecList = {}
local Players = game.Players:GetPlayers()
for i, v in ipairs(Players) do
print(SpecList)
print("Outside")
local Value = v.Character:WaitForChild("Value").Value
if Value ~= 2 and v.Name ~= Player.Name then
print("Inside")
table.insert(SpecList, v)
end
end
game.Players.PlayerAdded:Connect(function()
local Players = game.Players:GetPlayers()
for i, v in ipairs(Players) do
print(SpecList)
print("Outside2")
local Character = v.Character or v.CharacterAdded:Wait()
local Value = Character:WaitForChild("Value").Value
local position = table.find(SpecList, v)
if Value ~= 2 and position == nil then
print("Inside2")
table.insert(SpecList, v)
print(SpecList)
local Pos = table.find(SpecList, Player)
if Pos ~= nil then
table.remove(SpecList, Pos)
end
end
end
end)
I had to add the part with removing the player because attempting to detect if the player was the one being checked would prevent anything from being added.
this script removes the player from SpecList if the value changes (I assume you want that) and it doesn’t use RunService:
local SpecList = {}
local function func(plr:Player)
if plr.Name ~= Player.Name then
local char = plr.Character or plr.CharacterAdded:Wait()
local value:NumberValue = char:WaitForChild("Value")
if value.Value ~= 2 then
table.insert(SpecList, plr)
end
value.Changed:Connect(function()
if value.Value == 2 then
local pos = table.find(SpecList, plr)
if pos then
table.remove(SpecList, pos)
end
else
table.insert(SpecList, plr)
end
end)
end
end
for _,plr in game.Players:GetPlayers() do
func(plr)
end
game.Players.PlayerAdded:Connect(function(plr)
func(plr)
end)