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.
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
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)