How would I get a random player from "i in pairs" if they meet a requirement

I’m attempting to get all players, check if they have that rank and magnitude - if they meet these requirements, then it’ll pick a random player that meets the requirement, and will teleport them - but I’m not too sure on why it’s not functioning as intended. The error message is attempt to get length of a Instance value which is at local choseni = i[math.random(1, #i)]. What’s the issue here?

	ui.Bring.MouseButton1Click:Connect(function()
		local tpBlock = game.Workspace:FindFirstChild("IntTp"..val)
		for _, i in pairs(game.Players:GetPlayers()) do
			local magnitude = (i.Character:WaitForChild("HumanoidRootPart").Position - game.Workspace.IntChecker.Position).Magnitude
			if i:GetRankInGroup(5301253) <= 6 and magnitude <= 20 then	
				local choseni = i[math.random(1, #i)]
				choseni.Character:WaitForChild("Humanoid").Jump = true
				wait(.3)
				choseni.Character:WaitForChild("HumanoidRootPart").Position = tpBlock.Position
			end
		end
	end)
1 Like

Hey @Doqee, “i” is not a table, it is a player instance, and therefore you cannot get the length of the table by writing “#i”. Another issue here is that you want to get a random player that meets the requirement, but in choosing a random player from the list of players, you aren’t choosing a player that meets the requirements. Instead the best way of going about this is creating a whitelist table and choosing a random value from that:


ui.Bring.MouseButton1Click:Connect(function()
	local players = game.Players:GetPlayers()
	local whiteList = {}
	for index,value in pairs(players) do
   		local magnitude = (value.Character:WaitForChild("HumanoidRootPart").Position - game.Workspace.IntChecker.Position).Magnitude
		if value:GetRankInGroup(5301253) <= 6 and magnitude <= 20 then
			table.insert(whiteList, value)
		end
    end

	local tpBlock = game.Workspace:FindFirstChild("IntTp"..val)
	local currentPlayer = whiteList[math.random(1,#whiteList)]
	currentPlayer.Character:WaitForChild("Humanoid").Jump = true
	wait(.3)
	currentPlayer.Character:WaitForChild("HumanoidRootPart").Position = tpBlock.Position
end
1 Like

Oh, I get it - thank you for your assistance!

1 Like

i refers to the actual Player Instance contained in game.Players, not the table of players. You need to get the length of game.Players:GetPlayers() instead to determine how many players are in the game.

Also, a note on the efficiency of your code. I suggest rearranging your conditional statement from

if i:GetRankInGroup(5301253) <= 6 and magnitude <= 20 then	

to

if magnitude <= 20 and i:GetRankInGroup(5301253) <= 6 then	

Once one of the conditions in an and condition is non-truthy, the conditions to the right will not even be considered. It’s likely that the character will be farther than 20 studs away, so if you arrange your conditional statement in this way, the expensive (and relatively slow) HTTP request needed for GetRankInGroup to function is only ever sent if they meet the computationally cheaper distance check.