local players = Players:GetPlayers()
for i, v in pairs(players) do
if v:FindFirstChild("Values") then
if v.Values.InSubway.Value == true then
RE.ChangeLighting:FireClient(v)
RE.PlayLocalSounds:FireClient(v)
task.wait(1)
if #v == 0 then
elseif #v == 1 then -->> Error Here <<--
local character = v.Character or v.CharacterAdded:Wait()
character:PivotTo(TPPart.CFrame)
elseif #v >= 2 then
local randomPlayer = v[math.random(1, #v)]
local char = randomPlayer.Character or randomPlayer.CharacterAdded:Wait()
char:PivotTo(TPPart.CFrame)
end
else
-- Don't Change Lighting if this value set to false
end
end
end
I’m trying to get a random player using #, but that doesn’t seem to work.
In the output it keeps saying: "attempt to get length of a Instance value"
local players = Players:GetPlayers()
function getplayersEnabled()
local plrswithenabled = {}
for i,v in pairs(players) do
if v:FindFirstChild("Values") and v.Values.InSubway.Value == true then
table.insert(plrswithenabled,v)
end
end
return plrswithenabled
end
for i, v in pairs(players) do
if v:FindFirstChild("Values") and v.Values.InSubway.Value == true then
RE.ChangeLighting:FireClient(v)
RE.PlayLocalSounds:FireClient(v)
task.spawn(function()
task.wait(1)
local PlayersWithEnabledValue = getplayersEnabled()
if PlayersWithEnabledValue == 1 then
local character = v.Character or v.CharacterAdded:Wait()
character:PivotTo(TPPart.CFrame)
elseif PlayersWithEnabledValue >= 2 then
local randomPlayer = players[math.random(1, #players)]
local char = randomPlayer.Character or randomPlayer.CharacterAdded:Wait()
char:PivotTo(TPPart.CFrame)
end
end)
end
end
local players = Players:GetPlayers()
function getplayersEnabled()
local plrswithenabled = {}
for i,v in pairs(players) do
if v:FindFirstChild("Values") and v.Values.InSubway.Value == true then
table.insert(plrswithenabled,v)
end
end
return plrswithenabled
end
for i, v in pairs(players) do
if v:FindFirstChild("Values") and v.Values.InSubway.Value == true then
RE.ChangeLighting:FireClient(v)
RE.PlayLocalSounds:FireClient(v)
task.spawn(function()
task.wait(1)
local PlayersWithEnabledValue = getplayersEnabled()
local randomPlayer = v[math.random(1, #v)] -->> Error Here <<--
local character = randomPlayer.Character or randomPlayer.CharacterAdded:Wait()
character:PivotTo(TPPart.CFrame)
end)
end
end
(i got rid of if statements because I realized I didn’t need them)
It looks great, but on the line where it says "Error Here" the output still says: "attempt to get length of a Instance value"