A problem with mutliple loops inside of eachother

Hello everyone!
I am trying to make a system, that shows the amount of “handshakes” two specific players have. By “handshakes” I mean their friends in between.
For example, Player A has Player B in his friend list, and Player B has Player C in his friend list
That means, there’s 1 handshake between Player A and Player B and 2 handshakes between Player A and Player C.
Ok, the problem I’m having is the loops. There’s too much of them, and I want to automatize them.

local players = game.Players
local player = players.LocalPlayer
local plrid = player.UserId
local lookingforid = 19919175 -- some random player userid :)
local isfound = false
local handshakes = 0
local list = {}
local function getfriendid(page)
	local tb = {}
	repeat
		local info = page:GetCurrentPage()
		for i, friendInfo in pairs(info) do
			table.insert(tb, friendInfo.Id)
			if table.find(list, friendInfo.Id ) == nil then
				table.insert(list, friendInfo.Id)
			end
		end
		if not page.IsFinished then 
			page:AdvanceToNextPageAsync()
		end
	until page.IsFinished
	return tb
end


local function getfriends(plri)
	local success, friendPages = pcall(function() return players:GetFriendsAsync(plri) end)
	if success then
		return getfriendid(friendPages)
	else
		return nil 
	end
end

 for i, v in pairs(getfriends(plrid)) do
if isfound == false then
 if table.find(list, v) == nil then
if v ~= lookingforid then
table.insert(list, v)
			for i, v1 in pairs(getfriends(v)) do
				if table.find(list, v1) == nil then
					if v1 ~= lookingforid then
						table.insert(list, v1)
						else
							isfound = true
							handshakes = 2
end
end
end
			else
				isfound = true
				handshakes = 1
end
end
end
end

So, I’ve tried to make it with my own hands, but that’s too unefficient. Is there anyway I could automatize these loops in a loops in a loops in a loops… and etc.
I’ll be very thankful, if someone helps me.

If you mean something like a recurring function, then yes it is quite possible (and common).
For example, if you had the friends list for Player A, and you wanted to find any other player’s ID in it, then you’d make a function that takes in that friends list, loops through it, returns a result, and if no result is found, loop through the friends list of the players in that friends list.
To prevent infinite loops, you’d want to save a cache of friends lists checked (using the UserID of the player with that friends list), so that if you looped through Player A’s friends, found nothing, looped through Player B’s friends, and also found nothing, it wouldn’t just loop back through Player A’s friends again.
Here’s what that would look like:

local function recursiveGetHandshakes(startPlr, targetPlr)
	local checkedIDs = {}
	local handshakeCount = 1
	local targetFound = false
	
	local function recursiveCheck(plr.UserId)
		-- Skip if we already checked this player
		for _, id in checkedIDs do
			if id == plr.UserId then return end
		end
		-- Check if any of this player's friends match
		local friendsList = getfriends(plr.UserId)
		for _, friend in friendsList do
			if friend.UserId == targetPlr.UserId then
			targetFound = true
			return
			end
		end
		-- Add to checkedIDs table so we don't check this player again
		table.insert(checkedIDs,plr.UserId)
		-- Increment handshakeCount as we are checking a new friends list
		handshakeCount += 1
		
		-- Go through friends list of this player's friends
		for _, friend in friendsList do
			recursiveCheck(friend)
			if targetFound then return end -- Return if we found the target player
		end
	end
	recursiveCheck(startPlr)
	
	return handshakeCount
end

I have a good feeling that this either doesn’t work or isn’t what you’re looking for. Please inform me if so.

Thanks for trying to help, but, I don’t think you’ve got my question right.
What If there’s no needed PlayerID in the other Players Friends, their Friends and so go on,
Can I, like, do the loop in a loop in a loop in a loop… not manually, Is there a way?

1 Like

This is what I used a recursive function for, it will keep looping until every call of it is returned.

Can you elaborate on this? I’m not following.

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