Returning a nil value

Hello, I’m making a system that randomly chooses a player from a player’s friends list and then loads the friend’s character. I had everything working until I decided to pick multiple friends instead of just one. My issue is that the variable randomFriend is returning as nil everytime I call the function. I’ve searched the code for any bugs and just didn’t figure it out. Idk if my syntax is wrong or I have some other random problem even though my script was just working fine earlier.

And also another thing. I’ve been trying to make it so that if all of the player’s friends were already chosen, then the randomFriend variable will exit the while loop and return nil but I’m not sure how I would do this. Thank you in advance.

Here is my a piece of my script

local usernames = {}
for item, _pageNo in iterPageItems(friendPages) do
	table.insert(usernames, item.Username)
end

local chosenFriends = {}
local function selectRandomFriend()
	local randomIndex = math.random(1, #usernames)
	local randomFriend = usernames[randomIndex]

	for i, chosenFriend in ipairs(chosenFriends) do	
		if randomFriend ~= chosenFriend then 
			table.insert(chosenFriends, randomFriend)   --error should be happening right here
			return randomFriend 
		end
		
		while task.wait() do
			--If the friend was already chosen then pick another one until it was not already chosen
			if randomFriend == chosenFriend then
				randomIndex = math.random(1, #usernames)
				randomFriend = usernames[randomIndex]
				--What if all friends have already been chosen? Still need to implement this.
				
			else --If randomFriend is not equal to a friend that was already chosen, then return the randomFriend.
				table.insert(chosenFriends, randomFriend)
				return randomFriend
			end
		end	
	end
end

Firstly, try adding some print statements to see whats happening

Im not exactly sure why its returning nil, but to check if all friends are chosen, you could try checking if every username in the usernames table is in the chosenFriends table. (It would however be very ineffecient if you do a double for loop. I swear there should be a better way to check)

edit: aha found it How do you check if instance is in a table? - #2 by Orbular3

nvm im stupid I see

See, if you start with ChosenFriends as blank, then you wont ever enter the loop, and would thus be unable to add a friend into it. You should add a special case if statement checking if ChosenFriends is empty, and if so, simply add in whatever RandomFriend is

Haha didn’t know about table.find that’s a game changer. As for the first comment idk what testing that would do since the chosenFriends table starts out as an empty table and never has any players added to it since randomFriend is nil. For the 2nd comment, I believe I’ve already tested this by doing if randomFriend ~= chosenFriend or chosenFriend == nil then and I ended up getting the same result. I will check that out again and mess around with it.

1 Like

That wont work, as if the order of the players being added is different, these two tables wont be the same. Do a single for loop and table.find each of the element to see if all is added.

Read my edit (didn’t realize until later lol)

From what I think I’m seeing from print statements, the for loop is never initiating because the chosenFriends table is empty. So could I just add a placeholder into the chosenFriends table so the rest of the script would run normally?

I think I’m just overthinking this rn and it’s getting late. Can I get back to you tomorrow or some other time?

Hi, thanks for the help! I just got it working after some debugging and trying some of the things you mentioned. The problem was indeed the for loop which caused the randomFriends to be inserted into the chosenFriends table and then returned even if they were already chosen. I used table.find instead of a for loop and checked if the randomFriend already existed inside of the chosenFriends table. My final script looks like this:

local usernames = {}
for item, _pageNo in iterPageItems(friendPages) do
	table.insert(usernames, item.Username)
end

local chosenFriends = {}
local function selectRandomFriend()
	local randomIndex = math.random(1, #usernames)
	local randomFriend = usernames[randomIndex]
	print(randomFriend)
	if table.find(chosenFriends, randomFriend) == nil then
		table.insert(chosenFriends, randomFriend)
		return randomFriend 
	end
		
	while task.wait() do
		--If the friend was already chosen, pick another one until it was not already chosen
		if table.find(chosenFriends, randomFriend) ~= nil then
			randomIndex = math.random(1, #usernames)
			randomFriend = usernames[randomIndex]
			--What if all friends have already been chosen? Still need to implement this.
			for i, chosenFriend in ipairs(chosenFriends) do 
				--If all the chosenFriend is found in the usernames table then this means that all friends were already chosen. So, set randomFriend to nil and return nil.
				if table.find(usernames, chosenFriend) ~= nil then randomFriend = nil return randomFriend end

			end
				
		else --If randomFriend is not equal to a friend that was already chosen, then return the randomFriend.
			table.insert(chosenFriends, randomFriend)
			return randomFriend
		end
	end	
end```
Again thanks for the help!
1 Like

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