Cannot Get :IsFriendsWith() To Work

  1. What do you want to achieve?
    I want the script to check and see if one player is friends with another.

  2. What is the issue?
    It will not for some reason stay on the current player it is on. It will check a player against itself.

  3. What solutions have you tried so far?
    I have tried almost everything I can think of. I don’t know why it isn’t working and I have tried looking to see if anyone has had similar issues.

In the screenshot below you can see what I mean by it just checks the player against itself.
image


local Plyrs = game.Players:GetChildren() -- Getting the main player to check and change stats of.

for i = 1, #Plyrs do -- Starting the loop
			local friendswith = {} -- Table tosee what friends are in the game
			table.clear(friendswith)

			for i, v in pairs(game.Players:GetPlayers()) do -- This loop is to check the main player against every player in the server to see if they are playing with friends.
				print(v.Name.." "..Plyrs[i].Name) -- This print statement tells me who it is checking the main player against
				local ID = v.UserId
				if Plyrs[i]:IsFriendsWith(ID) then -- Checks to see if the two players are friends.
					table.insert(friendswith, v) -- Adds any friends to the table.
				end
			end
        end

Sorry if the code is a bit messy. I have gone thru like 30 versions trying everything to get this to work. Also, the function is fired at the end of a round. Not when the game first starts obviously.

I would use something like this:

game.Players.PlayerAdded:Connect(function(plr)
	local friendswith = {}
	
	for i, player in pairs(game.Players:GetPlayers()) do
		if player ~= plr then
			print(player.Name.." "..plr.Name)
			
			if plr:IsFriendsWith(player.UserId) then
				table.insert(friendswith, player)
			end
		end
	end
	
	print(friendswith)
end)

That wouldn’t work for what I need. It needs to be able to get all the players in the game and check to see who is friends with who. Not a join/leave system.

try:

local Plyrs = game.Players:GetChildren() -- Getting the main player to check and change stats of.

for i = 1, #Plyrs do -- Starting the loop
			local friendswith = {} -- Table tosee what friends are in the game
			table.clear(friendswith)

			for index, v in pairs(game.Players:GetPlayers()) do -- This loop is to check the main player against every player in the server to see if they are playing with friends.
				print(v.Name.." "..Plyrs[i].Name) -- This print statement tells me who it is checking the main player against
				local ID = v.UserId
				if Plyrs[i]:IsFriendsWith(ID) and Plyrs[i] ~= v then -- Checks to see if the two players are friends.
					table.insert(friendswith, v) -- Adds any friends to the table.
				end
			end
        end
4 Likes

Yep that worked. Not sure how but it did. Thank you.