How do I restrict a lobby to only friends under certain circumstances

I am currently creating a lobby system for my game. I would like it so that when pubvalue = 2 then only people who are friends with the owner of the lobby. I have tried directly getting the lobby owner’s ID and using checking if the player is a friend with Id = player.UserId and

local frnd = player:IsFriendsWith(Id)
		if pubvalue == 2 then if frnd == nil then return end end

But this still allows anyone to join the lobby.
Here is my current script:

_G.plyrs1 = {}

local sgui = script.Parent.SurfaceGui
local owner = sgui.Owner
local requirement = sgui.Requirement
local status = sgui.Status
local publicity = sgui.Publicity
local teleDest = game.Workspace.TeleDestination1
local pubvalue = game.Workspace.Walls.TeleWall1.SurfaceGui.Publicity.Value.Value

script.Parent.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if not (player and player.Character) then return end
	local Pgui = player.PlayerGui
	local menu = Pgui:WaitForChild("LobbyMaking1")
	
	if #_G.plyrs1 == 0 then
		owner.Text = player.Name .."'s Lobby"
		Id = player.UserId
		table.insert(_G.plyrs1, player)
		print(_G.plyrs1)
		print(#_G.plyrs1)
		status.Text = "Players: " .. #_G.plyrs1
		pubvalue = 3
		publicity.Text = "Private"
		player.Character:FindFirstChild("HumanoidRootPart").CFrame = teleDest.CFrame + Vector3.new(0,5,0)
		for _, child in ipairs(menu:GetChildren()) do
			child.Visible = true
		end
	else
		if pubvalue == 3 then return end
		local frnd = player:IsFriendsWith(Id)
		if pubvalue == 2 then if frnd == nil then return end end
		local idx = table.find(_G.plyrs1, player)
		if idx == nil then
			table.insert(_G.plyrs1, player)
			print(_G.plyrs1)
			print(#_G.plyrs1)
			status.Text = "Players: " .. #_G.plyrs1
			player.Character:FindFirstChild("HumanoidRootPart").CFrame = teleDest.CFrame + Vector3.new(0,5,0)
		end
	end
end)

I made plyrs1 global because I use it in other scripts.

Boosting because I haven’t found an answer yet.

:IsFriendsWith returns a bool (true/false). Just replace this part of the code with:

local frnd = player:IsFriendsWith(Id)
		if pubvalue == 2 and not frnd then return end

That worked but I needed to change pubvalue to a global variable, but Thanks!

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