Attempt to get length of a Instance value

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"

Any help is appreciated!

v is a player instance , not a table

1 Like

And how would I make it a table? Or would I not be able to?

1 Like

i think you’re meaning to use the i variable which is a number.
players is a table of players

1 Like

Well in this unique case, I only want to get the players that have InSubway.Value set to true.

How would I do this AND get still get a random player (within the players set to true)?

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
1 Like

what do you think about my solution mate?

1 Like
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"

I’m not sure how to solve this issue.

yeah change the v to players and it should be working fine (in the error line)

2 Likes
local randomPlayer = PlayersWithEnabledValue[math.random(1, #PlayersWithEnabledValue)]

Yes, I just changed it to this and it worked perfectly, thank you!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.