Why does the selection of a random player keep changing?

Hi there, I have a simple script that prints the magnitude between you and a random person. The issue is, is that the random person selected constantly changes if more than one person apart from you is in the server. I don’t know what else to try as I have no idea on why this could’ve had happened, but would like help regarding to it.

local Players = game:GetService("Players")
local players = {}

local PlayerPerson
local RandomPlayer

local function randomperson()
	repeat
		RandomPlayer = Players:GetPlayers()[math.random(#Players:GetPlayers())]
	until RandomPlayer.Name ~= "vxsqi"
	return RandomPlayer
end

local function LPlayer()
	PlayerPerson = game:GetService('Players').vxsqi
	return PlayerPerson
end

while true do	

	local pos = LPlayer().Character.HumanoidRootPart
	local secondpos = randomperson().Character.HumanoidRootPart

	local function mainperson()
		local posx = tonumber(pos.Position.X)
		local posy = tonumber(pos.Position.Y)
		local posz = tonumber(pos.Position.Z)
		local posxround = math.round(posx)
		local posyround = math.round(posy)
		local poszround = math.round(posz)
		local vector = Vector3.new(posxround,posyround,poszround)
		
		return vector

	end
	
	local function secondperson()
		local posx = tonumber(secondpos.Position.X)
		local posy = tonumber(secondpos.Position.Y)
		local posz = tonumber(secondpos.Position.Z)
		local posxround = math.round(posx)
		local posyround = math.round(posy)
		local poszround = math.round(posz)
		local vector = Vector3.new(posxround,posyround,poszround)

		return vector

	end

	local magnitude = (mainperson() - secondperson()).Magnitude
	print("The magnitude between "..LPlayer().Name.." and "..randomperson().Name.." is "..magnitude..".")
	
	if magnitude > 100 then
		print("valid")
	end
	task.wait(1)
end

This is because you are doing this in a while loop.

And how can I avoid this; how can I still have a randomly selected player, and return within the loop without changing the player?

When you find a player you break the while loop :

while true do	

	local pos = LPlayer().Character.HumanoidRootPart
	local secondpos = randomperson().Character.HumanoidRootPart

	local function mainperson()
		local posx = tonumber(pos.Position.X)
		local posy = tonumber(pos.Position.Y)
		local posz = tonumber(pos.Position.Z)
		local posxround = math.round(posx)
		local posyround = math.round(posy)
		local poszround = math.round(posz)
		local vector = Vector3.new(posxround,posyround,poszround)
		
		return vector

	end
	
	local function secondperson()
		local posx = tonumber(secondpos.Position.X)
		local posy = tonumber(secondpos.Position.Y)
		local posz = tonumber(secondpos.Position.Z)
		local posxround = math.round(posx)
		local posyround = math.round(posy)
		local poszround = math.round(posz)
		local vector = Vector3.new(posxround,posyround,poszround)

		return vector

	end

	local magnitude = (mainperson() - secondperson()).Magnitude
	print("The magnitude between "..LPlayer().Name.." and "..randomperson().Name.." is "..magnitude..".")
	
	if magnitude > 100 then
		print("valid")
	end

	if secondperson() and mainperson() then
         break
      end
end

I think you want to store the random person as a variable outside of the loop. it will never change after getting the first random person.

local randomperson = randomperson()

while true do
	local pos = LPlayer().Character.HumanoidRootPart
	local secondpos = randomperson.Character.HumanoidRootPart
      -- ...
	print("The magnitude between "..LPlayer().Name.." and "..randomperson.Name.." is "..magnitude..".")