Problem with "IsFriendsWith"

So I am trying to make a friends only lobby but I got a problem that it doesn’t work.

A video clearly showing that they are friends:

The Code:

local Button = script.Parent
local RS = game:GetService("ReplicatedStorage")
local SoundService = game:GetService("SoundService")
local Players = game:GetService("Players")
local plr = Players.LocalPlayer

Button.Activated:Connect(function()
	if Button.Parent.Players.Text == "1/1" or Button.Parent.Players.Text == "2/2" or Button.Parent.Players.Text == "3/3" or Button.Parent.Players.Text == "4/4" then print("full") SoundService.SFX.Error.error:Play() return end
	
	if Button.Parent:GetAttribute("OnlyFriends") then
		local UserId = Players:GetUserIdFromNameAsync(Players:FindFirstChild(Button.Parent:GetAttribute("Owner")))
		print(Button.Parent.Parent.Parent.Parent.Parent.Parent)
		print(Players:FindFirstChild(Button.Parent:GetAttribute("Owner")))
		if Button.Parent.Parent.Parent.Parent.Parent.Parent:IsFriendsWith(UserId) then 
			print("friends")
			Button.Parent.Visible = false
			plr.PlayerGui.Party.FindParty.Visible = false
			plr.PlayerGui.Party.Party[Button.Parent.Title.Text].Visible = true
			RS:WaitForChild("Events").Communicate:FireServer("JoinParty",Button.Parent, Button.Parent.Title.Text)	
		else
			print("not friends")
			SoundService.SFX.Error.error:Play()
			Button.Text = "Only for friends..."
			task.delay(1,function()
				Button.Text = "Join"
			end)
		end
		
	else
		Button.Parent.Visible = false
		plr.PlayerGui.Party.FindParty.Visible = false
		plr.PlayerGui.Party.Party[Button.Parent.Title.Text].Visible = true
		RS:WaitForChild("Events").Communicate:FireServer("JoinParty",Button.Parent, Button.Parent.Title.Text)	
	end
	
end)

I mean it gets both the players correctly so I don’t get why it isn’t working.

Tell me if you need more info.

2 Likes

Why are you doing this

shouldn’t you do something like

for _, player: Player in ipairs(Players:GetPlayers()) do
   if player:IsFriendsWith(UserId)
   end
end
1 Like

huh why should I do that? It also doesn’t work

(just wanna boost this up again because its still not fixed)

Just use your plr variable in here, since you are using a local script

This also didn’t work, already tried that.

could you maybe print what UserId is?

it was just a few numbers, like I think 5 or 6 numbers (which I guess was the UserId)

Oh my god you dont need the “ipairs” for the loop to work

OP could also just consider doing something like this:

Button:FindFirstAncestorOfClass(“Player”)

I think thats alot cleaner and more optimized

1 Like

I don’t think that this solves the problem. And making the code cleaner is just helpful if it even works

yes but Button:FindFirstAncestorOfClass(“Player”) is more optimized than GetPlayers loop because your loop needs to go thru all of the players in the server meanwhile Button:FindFirstAncestorOfClass(“Player”) just gets the player by looking at ancestors of Button instance

yea I know that but I also never used the code Stephenesta_aqui suggested me

Then maybe a different approach?

Button.Activated:Connect(function()
	if Button.Parent.Players.Text == "1/1" or Button.Parent.Players.Text == "2/2" or Button.Parent.Players.Text == "3/3" or Button.Parent.Players.Text == "4/4" then print("full") SoundService.SFX.Error.error:Play() return end

	if Button.Parent:GetAttribute("OnlyFriends") then
		local Owner = Players:FindFirstChild(Button.Parent:GetAttribute("Owner"))
		if Owner then
			local UserId = Owner.UserId
			print(Button.Parent.Parent.Parent.Parent.Parent.Parent)
			print(Players:FindFirstChild(Button.Parent:GetAttribute("Owner")))
			if plr:IsFriendsWith(UserId) then 
				print("friends")
				Button.Parent.Visible = false
				plr.PlayerGui.Party.FindParty.Visible = false
				plr.PlayerGui.Party.Party[Button.Parent.Title.Text].Visible = true
				RS:WaitForChild("Events").Communicate:FireServer("JoinParty",Button.Parent, Button.Parent.Title.Text)	
			else
				print("not friends")
				SoundService.SFX.Error.error:Play()
				Button.Text = "Only for friends..."
				task.delay(1,function()
					Button.Text = "Join"
				end)
			end
		end
	else
		Button.Parent.Visible = false
		plr.PlayerGui.Party.FindParty.Visible = false
		plr.PlayerGui.Party.Party[Button.Parent.Title.Text].Visible = true
		RS:WaitForChild("Events").Communicate:FireServer("JoinParty",Button.Parent, Button.Parent.Title.Text)	
	end
end)
2 Likes

Thanks this seem to work. What exactly did you change?

1 Like

Basically just searching for the owner in Players and getting their UserId (since they should be a player in there if they were found)

2 Likes

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