--when verifying friendships, we just check both the caller and the target's friend lists to see if the IDs are found in both lists


local friendlists = {} --Key: person's username. Value: table of online friends' user IDs

function CheckFriendship(caller,target)
	if caller == nil or target == nil or friendlists[caller.Name] == nil or friendlists[target.Name] == nil then return false end
	local f1 = false
	local f2 = false
	for _,v in pairs(friendlists[caller.Name]) do
		if v == target.userId then
			f1 = true
			break
		end
	end
	for _,v in pairs(friendlists[target.Name]) do
		if v == caller.userId then
			f2 = true
			break
		end
	end
	if f1 == true and f2 == true then
		return true
	else
		return false
	end
end

game.ReplicatedStorage.RemoteFunctions.SendFriendsOnlineData.OnServerEvent:connect(function(plr,tbl) --client should send a table of all friend userIDs
	if type(tbl) == "table" and #tbl <= 200 then
		for _,v in pairs(tbl) do
			if type(v) ~= "number" then return end
		end
		friendlists[plr.Name] = tbl
	end
end)

game.ServerStorage.RemoteFunctions.PlayersAreFriends.OnInvoke = function(caller,target)
	return CheckFriendship(caller,target)
end


game.Players.PlayerRemoving:connect(function(plr)
	friendlists[plr.Name] = nil
end)
