How would I check if a player is friends with the owner

I am making an admin system and I want an option for the owner’s friends to have admin.

local admins = {
	{
		ID = 1578920035,
		Rank = 5
	},
}

local friendCommands = true
local friendCommandRank = 4

game.Players.PlayerAdded:Connect(function(player)
	if friendCommands == true then
		if player.UserId == game.CreatorId then
			local friends = game.Players:GetFriendsAsync(player.UserId)
			for i, v in pairs(friends) do
				table.insert(admins, {ID = i.UserId, Rank = friendCommandRank})
			end
		end
	end
end)

Not sure if I’m doing something wrong but I’ve searched the dev forum and I can’t find anything else on the wiki about get friend async.

Looking at the FriendPages it looks to me that i.UserId should be changed to i.Id as UserId does not exist in the FriendPages object. One more thing to note is you should probably check that the place is owned by a player not a group. You can do this by adding this by changing

if player.UserId == game.CreatorId then

into this

if game.CreatorType == Enum.CreatorType.User and player.UserId == game.CreatorId then

EDIT:
I just noticed it does not appear like you are using GetCurrentPage() which means the loop will not work. I’d recommend using this function from the devhub which you can just put at the top of the script.

local function iterPageItems(pages)
	return coroutine.wrap(function()
		local pagenum = 1
		while true do
			for _, item in ipairs(pages:GetCurrentPage()) do
				coroutine.yield(item, pagenum)
			end
			if pages.IsFinished then
				break
			end
			pages:AdvanceToNextPageAsync()
			pagenum = pagenum + 1
		end
	end)
end

And then you can change your pairs loop to this

for item, pageNo in iterPageItems(friendPages) do
    table.insert(admins, {ID = i.UserId, Rank = friendCommandRank})
end
2 Likes

Ah thanks, I will get back to you if it works after I’ve done some testing :+1:

This is what you’re looking for!

4 Likes
game.Players.PlayerAdded:Connect(function(player)
	if friendCommands == true then
		if game.CreatorType == Enum.CreatorType.User then
			if player:IsFriendsWith(game.CreatorId) then
				table.insert(admins, {ID = player.UserId, Rank = friendCommandRank})
			end
		end
	end
end)

Could this work?

3 Likes

You’d have to remove the line where you check if they’re the owner (and the end that goes with it), then I’d assume so! :slight_smile:

Misread that line!

1 Like

Which line? The creator bit checks to see if the creator type is a player not a group.

The code you sent looks fine. Have you tried testing it to confirm it works?

Yes I have but it doesn’t seem to work.

Try putting a few prints in around the place for debugging measures. That can help you determine where the script breaks.

1 Like

I believe I have fixed it now, thanks for all the help!